JAvaアルゴリズムの再帰


package com.hym.test;

public class RecursiveCalNum {
	public int calNum(int num, int baseValue) {
		num--;
		if (num <= 0) {
			return baseValue;
		} else {
			int theValue = baseValue + 2;
			return calNum(num, theValue);
		}
	}

	public static void main(String[] args) {
		RecursiveCalNum cal = new RecursiveCalNum();
		int k = cal.calNum(8, 10);
		System.out.println("k =  " + k);
	}
}


public class CalculateTheNum {
	public int calNum(int n) {
		if (n <= 1) {
			return 1;
		} else {
			return n * calNum(n - 1);
		}
	}

	public int calByFor(int n) {
		if (n <= 1) {
			return 1;
		}

		int theValue = n;
		for (int i = n; i > 1; i--) {
			theValue = theValue * (i - 1);
		}
		return theValue;
	}

	public static void main(String args[]) {
		CalculateTheNum cal = new CalculateTheNum();
		System.out.println(cal.calNum(-1));
	}
}

1.再帰には必ず終了する境界条件がある.そうしないと、デッドサイクルになる.
2.再帰はいずれもループで実現できる.
3.
http://baike.baidu.com/view/96473.htm?func=retitle