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?
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;
}
}