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