一只青蛙一次可以跳上1級台階,也可以跳上2級……它也可以跳上n級。求該青蛙跳上一個n級的台階總共有多少種跳法。


// test14.cpp : 定義控制台應用程序的入口點。
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include<cctype>
#include <vector>
#include<exception>
#include <initializer_list>
using namespace std;


class Solution {
public:
	int jumpFloorII(int number) {
  //第一種方法 算法復雜度太高
		int target = 0;
		if (number < 2)
			return 1;
		
		for (int i = number - 1; i >= 0;i--)
			target +=jumpFloorII(i);
		return target;
		
	 //第二種方法 節省空間,時間快
		
	}
};

int main()
{
	int num ;
	Solution so;
	while (cin>>num)
	{
		cout << "所求結果是: "  ;
		cout << so.jumpFloorII(num) << endl;
		cout << endl;
	}
	
	return 0;
}

注意:其方法和一次只能跳一個或者兩個台階類似,第一次跳1個,還有f(n-1)個方法;第一次跳2個,還有f(n-2)中方法;第一次跳3個,還有f(n-3)種方法。。。。。。。。。。。。。累加即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM