递归求n的阶乘——Java语言描述

public class RecursiveAlgorithm {
	StringBuffer buffer = new StringBuffer();
	public int factorial(int n) {
		if (n <= 1) {
			buffer.append(n);
			System.out.println(buffer.toString());
			return 1;
		}
		buffer.append(n).append(" * ");
		return n * factorial(n - 1);
	}
}
public class Test {
	public static void main(String[] args) {
		RecursiveAlgorithm recursive = new RecursiveAlgorithm();
		int pruduct = recursive.factorial(12);
		System.out.println("pruduct="+ pruduct);
	}
}

递归求阶乘输出结果:

12 * 11 * 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1

pruduct=479001600

 

Hello , World !

感谢所有!

你可能感兴趣的:(java)