- Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathBasicCombination.java
More file actions
Latest commit
43 lines (35 loc) · 1.16 KB
/
Copy pathBasicCombination.java
File metadata and controls
43 lines (35 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
packagealgorithm.basicMath;
importorg.junit.Test;
importstaticorg.hamcrest.CoreMatchers.is;
importstaticorg.junit.Assert.assertThat;
publicclassBasicCombination {
/*
TASK
n개의 서로 다른 원소 중 r개의 원소를 순서없이 선택하는 방법의 수를 구한다.
*/
@Test
publicvoidtest() {
assertThat(getByRecursion(0, 0), is(1));
assertThat(getByRecursion(1, 0), is(1));
assertThat(getByRecursion(2, 1), is(2));
assertThat(getByRecursion(8, 3), is(56));
assertThat(getByDp(0, 0), is(1));
assertThat(getByDp(1, 0), is(1));
assertThat(getByDp(2, 1), is(2));
assertThat(getByDp(8, 3), is(56));
}
publicintgetByRecursion(intn, intr) {
if (r == 0 || n == r) {
return1;
}
returngetByRecursion(n - 1, r - 1) + getByRecursion(n - 1, r);
}
publicintgetByDp(intn, intr) {
intcache[][] = newint[10][10];
if (r == 0 || n == r) {
return1;
}
if (cache[n][r] != 0) returncache[n][r];
returncache[n][r] = getByDp(n - 1, r - 1) + getByDp(n - 1, r);
}
}