Algorithm

[BOJ] 11050번 : 이항 계수 1

rudgh99_algo 2024. 9. 24. 06:24

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

반복문으로 구현한 코드를 가져왔다. 새롭다.