C语言宿舍分配方案计算:n位同学分配宿舍方案数
#include <stdio.h>
// 计算组合数C(n, m) int combination(int n, int m) { if (m == 0 || n == m) { return 1; } else { return combination(n - 1, m) + combination(n - 1, m - 1); } }
// 计算宿舍分配方案数 int calculateDormAllocation(int n) { int count = 0; for (int i = 1; i <= n; i++) { count += combination(n, i); } return count; }
int main() { int n; printf("请输入同学的数量:"); scanf("%d", &n); int result = calculateDormAllocation(n); printf("一共有%d种宿舍分配方案。\n", result); return 0; }
原文地址: https://www.cveoy.top/t/topic/o7ZQ 著作权归作者所有。请勿转载和采集!