标签:允许 个人 space 楼梯 cst std turn amp stream
题目:有一个人准备开始爬楼梯,假设楼梯有n个,这个人只允许一次爬一个楼梯或者一次爬两个楼梯,请问有多少种爬法?
这个人爬N阶楼梯的方法数=爬N-1阶的方法数+爬N-2阶的方法数
明显是一个递归的形式
中止条件是 爬一阶的楼梯只要一种方法 若阶数小于0 就不算一种方法 返回 0;
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 int coun=0; 6 7 int f(int n) 8 { 9 if(n == 0) 10 return 1; 11 else if(n == -1) 12 return 0; 13 else 14 return f(n-2) + f(n-1); 15 16 } 17 18 int main() 19 { 20 int num; 21 while(scanf("%d",&num)!=EOF){ 22 printf("%d\n",f(num)); 23 } 24 return 0; 25 }
标签:允许 个人 space 楼梯 cst std turn amp stream
原文地址:http://www.cnblogs.com/16-CHQ/p/6664659.html