fork download
  1. #include <stdio.h>
  2.  
  3. /* 階乗を求める関数(再帰なし) */
  4. int fact(int n)
  5. {
  6. int result = 1;
  7. int i;
  8.  
  9. for(i = 1; i <= n; i++) {
  10. result *= i;
  11. }
  12.  
  13. return result;
  14. }
  15.  
  16. /* 組み合わせ nCr を求める関数 */
  17. int comb(int n, int r)
  18. {
  19. return fact(n) / (fact(r) * fact(n - r));
  20. }
  21.  
  22. int main(void)
  23. {
  24. printf("5C2 = %d\n", comb(5, 2));
  25. printf("6C3 = %d\n", comb(6, 3));
  26. printf("7C4 = %d\n", comb(7, 4));
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
5C2 = 10
6C3 = 20
7C4 = 35