백준 11050번 이항 계수 1

< 백준 11050번 이항 계수 1 - 마포 코딩박 >

사용한 알고리즘: 구현


 순열조합의 nCk 를 구현하는 문제였습니다.

문제풀이는 다음과 같습니다.

(1) (코드 4~11)
 nCk = n!/k!(n-k)! = n*(n-1)*...*(n-k+1) / k! 입니다.
 입력되는 n 의 최대값이 10 으로 충분히 작으므로 위를 단순 계산해주는 함수를 만들었습니다.

#include <bits/stdc++.h>
using namespace std;
int Combi(int n, int k){
int temp1 = 1;
int temp2 = 1;
for(int i=n; i>n-k; i--) temp1*=i;
for(int i=1; i<=k; ++i) temp2*=i;
return temp1/temp2;
}
int main(){ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
int N, K;
cin >> N >> K;
cout << Combi(N,K) << '\n';
return 0;
}
view raw BOJ 11050.cpp hosted with ❤ by GitHub


댓글