Skip to content

Instantly share code, notes, and snippets.

@peter279k
Created May 24, 2017 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peter279k/5c9f836f9249d0e2fab84946564174ed to your computer and use it in GitHub Desktop.
Save peter279k/5c9f836f9249d0e2fab84946564174ed to your computer and use it in GitHub Desktop.
TQC+ JAVA
import java.util.Scanner;
public class JPA03 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
int sum = 1;
System.out.print("Input m:");
int m = keyboard.nextInt();
while(true) {
if(m < 0 || m == 999) {
break;
}
System.out.print("Input n:");
int n = keyboard.nextInt();
System.out.println("Ans (尾端遞迴) : " + factorial(m, n, sum));
System.out.println("Ans (迴圈) : " + factoLoop(m, n));
System.out.print("Input m:");
m = keyboard.nextInt();
}
}
public static int factorial(int m, int n, int sum) {
if(n == 0) {
return sum;
}
return factorial(m, n-1, m * sum);
}
public static int factoLoop(int m, int n) {
int sum = 1;
for(int index=1;index<=n;index++) {
sum *= m;
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment