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?
假设你在爬一个楼梯,该楼梯有n阶,你有两种爬法,每次爬一阶或者两阶。请问你有多少种爬法到达楼梯顶部。
public int ClimbStairs(int n) {
if(n == 0)
return 0;
int pre = 0;
int cur = 1;
int tempCur = 0;
for (int i = 1; i <= n; i++)
{
tempCur = cur;
cur = pre + cur;
pre = tempCur;
}
return cur;
}
求斐波那契数列
static public int[] Fibonacci(int n)
{
int[] arr = new int[n+1];
arr[0] = 0;
arr[1] = 1;
for (int i = 2; i < n + 1; i++)
{
arr[i] = arr[i - 1] + arr[i - 2];
}
return arr;
}
时间: 2024-10-06 02:52:13