Climbing_Stairs
Description
You are climbing a stair case. It takes n steps to reach to the top.
你在爬楼梯。它需要n步才能到达顶点。
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
每次你可以爬1级或2级。有多少种不同的方法可以爬到顶端?
Input
NONE
Output
NONE
Sample Input 1
2
Sample Output 1
2
Sample Input 2
3
Sample Output 2
3
Hint
For EX1:
-
1 step + 1 step
-
2 steps
For EX2:
-
1 step + 1 step + 1 step
-
1 step + 2 steps
-
2 steps + 1 step
PS:1 <= n <= 45
其实这道题挺简单的,看一下你就懂了
思路:
从第三项开始,每一项都是前两项之和。
#include<stdio.h>
int main(void){
int n,i,num;
int a[200];
a[0]=1;
a[1]=2;
for(i=2;i<200;i++){
a[i]=a[i-2]+a[i-1];
}
while(scanf("%d",&n) != EOF){
printf("%d\n",a[n-1]);
}
return 0;
}```