[java]이항 계수 1 - 백준11050
문제
풀이
문제 해석을 하자면,
- 우리가 잘 아는 “이항계수”를 구하면 되는 간단한 문제이다.
풀이
- 단순히 수학적인 공식을 그대로 구현하면 끝나는 문제인데 그렇게 하지않고,
- 직접 조합 로직을 작성하여 구해보았다.
- 단, 당연히 이 풀이는 비효율이다. 그냥 조합을 좀 더 이해해보자는 취지로 구현했다.
코드
/**
* 이항계수 공식 그대로 구현하면 되는데, 그냥 조합 재귀로직 짜서 해봤다.
* 당연히 비효율적
*/
public static int result;
public static int N, K;
static void dfs(int depth, int idx) {
//base condition
if(depth==K){
result++;
}
//recursion
for(int i=idx; i<=N; i++){
dfs(depth+1, i+1);
}
}
public static void main(String[] args) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stk = new StringTokenizer(br.readLine(), " ");
N = Integer.parseInt(stk.nextToken());
K = Integer.parseInt(stk.nextToken());
dfs(0,1); // nCk
System.out.println(result);
}
느낀점
생략
댓글남기기