There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does not matter). Order does not matter means for n=4 {1 2 1},{2 1 1},{1 1 2} are considered same.
Input:
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, an integer N will be given.
Output:
Print number of possible ways to reach Nth stair.
Constraints:
1<=T<=1000
1<=N<=100
Example:
Input:
1
4
Output:
3
**For More Examples Use Expected Output**
这段时间打算用geekforgeekt练一下dp的题。
这道题思路比较简单,跟count stair不一样的是这道题只需要求出几种组合。思路是,step每次迈的步伐数组放在外层循环,内层循环放每一层楼梯。这样能够保证每个内层循环使用的步伐树是一致的,而且不会出现重复的情况。这里我觉得有必要具体说明一下,但是现在感觉还没想好怎么描述。
代码:
public static void main (String[] args)
{
//code
Scanner input = new Scanner(System.in);
int number = input.nextInt();
int[] inputArray = new int[number];
int[] result = new int[number];
for(int i=0;i<number;i++){
inputArray[i] = input.nextInt();
result[i] = compute(inputArray[i]);
}
for(int i=0;i<result.length;i++){
System.out.println(result[i]);
}
}
private static int compute(int stair){
if(stair <=0) return 0;
if(stair==1) return 1;
if(stair == 2) return 2;
int[] steps = {1, 2};
int[] dp = new int[stair+1];
dp[0] = 1;
for(int i=steps.length-1;i>=0;i--){
for(int j=1;j<dp.length;j++){
if(j-steps[i]>=0) dp[j] += dp[j-steps[i]];
}
}
return dp[stair];
}