403 尾端遞迴次方計算

import java.util.Scanner;
public class JPA403 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String args[]) {
int m;
int n;

System.out.print("Input m : ");
m=keyboard.nextInt();

    while(m!=999){
    System.out.print("Input n : ");
    n=keyboard.nextInt();
    System.out.println("Ans ( 尾端遞迴 ) : "+fact(m,n,1));
    System.out.println("Ans ( 迴圈 ) : "+loop(m,n,1));
    System.out.print("Input m : ");
m=keyboard.nextInt();
    }
    }

static int fact(int x,int y,int z){
if(y>0){
return fact(x,y-1,x*z);
}
else{
return z;
}
}

    static int loop(int x,int y,int z){
    while (y>0){
    z=z*x;
    y--;
    }
    return z;
    }
}

沒有留言:

張貼留言