1. 题目
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
- 
示例 1: 输入:n = 2 输出:2 解释:有两种方法可以爬到楼顶。 1 阶 + 1 阶 2 阶 
- 
示例 2: 输入:n = 3 输出:3 解释:有三种方法可以爬到楼顶。 1 阶 + 1 阶 + 1 阶 1 阶 + 2 阶 2 阶 + 1 阶 
提示:
1 <= n <= 45
2 思路与编程
当n=1时,f(1) = 1;
当n=2时,f(2) = 2;
当n=3时,f(3) = 3;
当n=4时,f(4) = 5;
当n=5时,f(5) = 8;
所以当n>2时,f(n) = f(n-1) + f(n-2);
            
            
              c
              
              
            
          
          #include <stdio.h>
#include <stdlib.h>
int climbstairs(int n)
{
    if (n == 0) return 0;
    if (n == 1) return 1;
    if (n == 2) return 2;
    return climbstairs(n -1) + climbstairs(n -2);
}
int main()
{
    int n;
    scanf("%d", &n);
    int result = climbstairs(n);
    printf("the result:%d\n", result);
    return 0;
}运行结果:
            
            
              c
              
              
            
          
          G3-3579:~/data/source/mianshi_code$ gcc climb_stairs.c 
G3-3579:~/data/source/mianshi_code$ ./a.out 
3
the result:3
G3-3579:~/data/source/mianshi_code$ ./a.out 
4
the result:5
G3-3579:~/data/source/mianshi_code$ ./a.out 
5
the result:8
G3-3579:~/data/source/mianshi_code$ ./a.out 
2
the result:2
G3-3579:~/data/source/mianshi_code$ ./a.out 
1
the result:1
G3-3579:~/data/source/mianshi_code$