[BOJ] 11050번 : 이항 계수 1
2024. 9. 24. 06:24ㆍAlgorithm
1. problem :
https://www.acmicpc.net/problem/11050
2. solution 1 :
#include <bits/stdc++.h>
using namespace std;
int n, k;
int factorial(int a) {
if (a == 0 || a == 1) return 1;
return a * factorial(a - 1);
}
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
int ans = factorial(n) / (factorial(n - k) * factorial(k));
cout << ans << '\n';
}
factorial 함수를 재귀적으로 구현했다.
3. solution 2 :
// Authored by : BaaaaaaaaaaarkingDog
// Co-authored by : -
// http://boj.kr/88189dab63974f8e9446dd9d960aea56
#include <bits/stdc++.h>
using namespace std;
int main(void){
ios::sync_with_stdio(0);
cin.tie(0);
int n,k;
cin >> n >> k;
int ret = 1;
for(int i = 1; i <= n; i++) ret *= i;
for(int i = 1; i <= k; i++) ret /= i;
for(int i = 1; i <= n-k; i++) ret /= i;
cout << ret;
}
source code 출처 : https://github.com/encrypted-def/basic-algo-lecture/blob/master/0x12/solutions/11050.cpp
basic-algo-lecture/0x12/solutions/11050.cpp at master · encrypted-def/basic-algo-lecture
바킹독의 실전 알고리즘 강의 자료. Contribute to encrypted-def/basic-algo-lecture development by creating an account on GitHub.
github.com
반복문으로 구현한 코드를 가져왔다. 새롭다.
'Algorithm' 카테고리의 다른 글
[BOJ] 6064번 : 카잉 달력 (0) | 2024.09.25 |
---|---|
[BOJ] 11051번 : 이항 계수 2 (0) | 2024.09.25 |
[BOJ] 15988번 : 1,2,3 더하기 3 (0) | 2024.09.23 |
[BOJ] 11653번 : 소인수분해 (1) | 2024.09.22 |
[BOJ] 1929번 : 소수 구하기 (1) | 2024.09.22 |