반응형

서로 다른 n개에서 r개를 택하여 일렬로 나열하는 방법의 수를 순열(permutation)이라 하고, nPr로 표시한다. 순열은 다음과 같은 식을 이용하여 구할 수 있다. 순열을 구하는 프로그램을 작성하라. n과 r은 사용자가 입력할 수 있도록 하라.

nPr=n(n-1)(n-2)...(n-r+1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
int main(void)
{
    int n, r, a, total;
    printf("n의 값: ");
    scanf("%d"&n);
    printf("r의 값: ");
    scanf("%d"&r);
    total=1;
    for(a=1;a<=r;a++)
    {
    total = total*n;
    n--;
    }
    printf("순열의 값은 %d입니다.\n", total);
    return 0;
}
cs


반응형

+ Recent posts