venusian

核心可以被归纳为C(n, m)(从n个对象中挑选出m个的方案个数)排列的代码实现

                 n!

C(n, m)= ————
m!(n-m)!

举例:

A1037:
问题描述
  试用递归方法编程求解下楼问题的方案数:从楼上到楼下共有h个台阶,下楼每步可走1个台阶、2个台阶或者3个台阶
  问可走出多少种方案数
输入格式
  一行,只有一个整数h,4<=h<=20(其他情况输出0)
输出格式
  一行,只有一个整数,表示下楼走法的总方案数
样例输入
4
样例输出
7
输入格式
  一行,只有一个整数h,4<=h<=20
输出格式
  一行,只有一个整数,表示下楼走法的总方案数
样例输入
4
样例输出
7

include

include <string.h>

include

include <math.h>

using namespace std;
//
int main() {
int tot;
cin >> tot;
int sum = 0;
if (tot >= 4 && tot<= 20)
for (int i = tot / 3; i >= 0; i--) {
int tot2 = tot - 3 * i;
for (int j = tot2; j >= 0; j--) {
int tot1 = tot2 - 2 * j;
int mul = 1;
for (int x = tot1 + i + j; x > tot1; x--)
mul *= x;
for (int x = i + j; x > 0; x--)
mul /= x;
for (int x = i + j; x > i; x--)
mul *= x;
for (int x = j; x > 0; x--)
mul /= x;
sum += mul;
}
}
cout<<sum;
return 0;
}

分类:

技术点:

相关文章:

  • 2021-04-24
  • 2021-10-03
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
  • 2022-12-23
  • 2022-03-03
猜你喜欢
  • 2021-08-03
  • 2022-02-15
  • 2021-10-03
  • 2022-01-07
  • 2021-12-08
  • 2021-12-01
  • 2022-12-23
相关资源
相似解决方案