青蛙跳

<pre name="code" class="java">  
  N塊石頭,青蛙在第1塊石頭上。青蛙跳,每次只能跳1個或者2個。問跳到第N塊石頭一共有多少種不同的跳法
 
 




public class FrogJump {

	public static int frogJumpWays(int n){
		if(n == 1 || n ==2){
			return 1;
		}
		int tmp = 1;
		int num1 = 1;
		int num2 = 1;
		for(int i = 2; i < n; i++){
			tmp = num2;
			num2 += num1;
			num1 = tmp;
		}
		return num2;
	}
	
	public static void main(String[] args){
		System.out.println(frogJumpWays(4));
		System.out.println(frogJumpWays(6));
		System.out.println(frogJumpWays(40));
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章