https://school.programmers.co.kr/learn/courses/30/lessons/120840
공식은 Hint에 나와있으니 이대로 하면 되는데 값이 너무 커져서 int형으로 하면 오버플로우가 나므로 double형을 사용하였다.
#include <string>
#include <vector>
using namespace std;
int solution(int balls, int share) {
int answer = 1;
double a = 1;
if(balls == share)
return 1;
for(int i = 1; i <= share; i++)
{
a /= (double)i;
a *= (double)(balls-i+1);
}
return (int)a;
}
#include <string>
#include <vector>
using namespace std;
int combi(int n, int r){
if(r == 0) return 1;
if(n == 1) return 1;
if(r >= n) return 1;
return combi(n-1, r) + combi(n-1, r-1);
}
int solution(int balls, int share) {
int answer = 0;
answer = combi(balls, share);
return answer;
}
https://ansohxxn.github.io/algorithm/combination/
조합(Combination) 구현하는 방법이다. 재귀함수를 사용한다.