70、Climbing Stairs

来源:互联网 发布:钥匙扣淘宝 编辑:程序博客网 时间:2024/05/17 23:18

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

 Dynamic Programming
    int climbStairs(int n) {        //有点类似Fabbaci算法,在计算机考研复习资料里面把此题作为动态规划的例题                if (n == 0)            return 0;        else if (n == 1)            return 1;        else         {            int a1 = 1, a2 = 2, temp;            for(int i = 3; i <= n; ++i)            {                temp = a2;                a2 = a1 + a2;                a1 = temp;            }            return a2;        }    }



0 0
原创粉丝点击