70. 爬樓梯

這麼簡單的題還要寫一下,我真菜

#include <cstddef>
#include <unordered_map>
#include <iostream>
#include <stack>
#include <string>
#include <ctime>
#include <vector>

using namespace std;
int climbStairs(int n) {
   if (2 == n)
   	return 2;
   if (1 == n)
   	return 1;
   return climbStairs(n - 1) + climbStairs(n - 2);
}
int right(int n )
{
   if(1==n)
   	return 1;
   if(2==n)
   	return 2;
   vector<int>a(n+1,0);
   a[0] = 0;
   a[1] = 1;
   a[2] = 2;
   for(int i=3;i<=n;++i)
   {
   	a[i]=a[i-1]+a[i-2];
   }
   return a[n];
}
int up = 999;
int down = 0;
int main(void)
{
   srand((unsigned)time(NULL));
   for(int i=0;i<1;  ++i)
   {
   	int temp=rand()%(up - down + 1 ) + down;
   	int temp1 = climbStairs(temp);	
   	int temp2 = right(temp);
   	if(temp1!=temp2)
   	{
   		cout<<"error"<<endl;
   		cout<<"temp="<<temp<<endl;
   		cout<<"testmethod="<<temp1<<endl;
   		cout<<"rightmethod="<<temp2<<endl;
   		return 0;
   	}
   }
   cout<<"right"<<endl;
   return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章