尾递归

http://www.nowamagic.net/librarys/veda/detail/2325

class LastRecursive {

	public static int factorial_tail(int n, int acc1, int acc2) {
		if (n < 2) {
			return acc1;
		} else {
			String str = String.format("factorial_tail(%d, %d, %d) \n", n - 1, acc2, acc1 + acc2);
			System.err.println(str);
			return factorial_tail(n - 1, acc2, acc1 + acc2);
		}
	}

	public void test(int n) {
		int rs = factorial_tail(n, 1, 1);
		String str = String.format("%d ", rs);
		System.err.println(str);
	}

}

猜你喜欢

转载自jis117.iteye.com/blog/2271846