/*
*一共有n层楼梯,人每次可以走1步或2步,一共有多少种走法?
*
*先找出n中有多少个数为1步,多少个数为2步 ,然后分别用排列组合求出对应组合数(例如1 1 1 2 2总共几种排列)
*/
class Test{
static int factorial(int n){//求n的阶乘
int result = 1;
for (int i = 1; i <= n; i++){
result *= i;
}
return result;
}
static int rank(int m, int n){ //在m个空位中选n个坐下的组合数
return factorial(m)/(factorial(n) * factorial(m - n));
}
public static void main(String[] args){
int n = Integer.parseInt(args[0]);
int result = 1;// 1 1 1 1 1 1
for(int i = n-1; i >= (n+1)/2; i--){
//1 1 1 1 2
//1 1 2 2
//2 2 2
int n1 = 2 * i - n;
int n2 = n - i;
int max = n1 > n2 ? n1 : n2;
result = result + rank(i, max);//rank:求出1 1 1 1 2 总组合数
}
System.out.println(result);
}
}
一共有n层楼梯,人每次可以走1步或2步,一共有多少种走法?
猜你喜欢
转载自blog.csdn.net/qq_36336003/article/details/79959722
今日推荐
周排行