fork download
  1. #include <stdio.h>
  2.  
  3. // 階乗を計算する関数
  4. int factorial(int n) {
  5. int result = 1;
  6. for(int i = 1; i <= n; i++) {
  7. result *= i;
  8. }
  9. return result;
  10. }
  11.  
  12. // 組合せを計算する関数
  13. int comb(int m, int k) {
  14. return factorial(m) / (factorial(k) * factorial(m - k));
  15. }
  16.  
  17. int main() {
  18. int m, k;
  19.  
  20. printf("mとkの値を入力してください(mは12以下): ");
  21. scanf("%d %d", &m, &k);
  22.  
  23. if(m <= 12 && k <= m && k >= 0) {
  24. int result = comb(m, k);
  25. printf("%d個の中から%d個を取り出す組合せは %d 通りです。\n", m, k, result);
  26. } else {
  27. printf("不正な入力です。mは12以下、0 <= k <= m にしてください。\n");
  28. }
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0s 5292KB
stdin
12 5
stdout
mとkの値を入力してください(mは12以下): 12個の中から5個を取り出す組合せは 792 通りです。