반응형
factorial 함수안에서 각 숫자의 팩토리얼값을 계산한다.
팩토리얼 계산은 n! = 1×2×3×4× ... ×n 이므로
for(i=1;i<=n;i++){
e=e*i;
}
로 간단하게 나타낼 수 있다.
scanf 에서 숫자를 받게 되는데,
첫번째 입력값이 n의 값이고 두번째 입력값이 r의 값이다.
combination 함수에서 nCr 의 계산을 해준다.
nCr = n! / r!*(n-r)! 이므로.
factorial(c)/(factorial(d)*factorial(c-d)) 값으로 만들어 주어 계산함.
아래부터는 소스예시.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <stdio.h> double factorial(double n); double combination(int c, int d); double factorial(double n){ double e=1, i; for(i=1;i<=n;i++){ e=e*i; } // printf("factorials %0.lf\n",e); return e; } double combination(int c, int d){ double combi; combi = factorial(c)/(factorial(d)*factorial(c-d)); // printf("Combination : %0.lf\n",combi); return combi; } int main (int argc, const char * argv[]) { double a,b,d; printf("Input the number :"); scanf("%lf %lf",&a,&b); printf("numbers : %0.lfC%0.lf\n",a,b); d = combination(a, b); printf("Combination answer : %0.lf\n",d); return 0; } |
반응형