402尾端遞迴階乘計算

/******
檔名:JPA04.java
題目:TQC+ JAVA6物件導向程式設計 參考答案
時間:2015/06
作者:fang shi mai
******/
import java.util.Scanner;

public class JPA04
{
    static Scanner keyboard = new Scanner(System.in);
    
    public static void main(String args[])
    {
        int N;
        
        while(true)
        {
            System.out.print("Inout n (0 <= n >=16):");
            N=keyboard.nextInt();
            if(N==999)
                break;
            if(N<=0 || N>=16)
                continue;
            System.out.printf("%d 的階乘(尾端遞迴) = %d\n",N,fac(N,1));
            System.out.printf("%d 的階乘(迴圈) = %d\n",N,loop(N,1));
        }
    }
    public static int fac(int a,int b)
    {
        if(a==0)
            return b;
        return fac(a-1,b*a);
    }
    public static int loop(int a,int b)
    {
        while(a>0)
        {
            b=b*a;
            a=a-1;
        }
        return b;
    }
}

沒有留言:

張貼留言