- Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathFactorialZeroCount.java
More file actions
Latest commit
54 lines (44 loc) · 1.14 KB
/
Copy pathFactorialZeroCount.java
File metadata and controls
54 lines (44 loc) · 1.14 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
44
45
46
47
48
49
50
51
52
53
54
packageexercise;
importorg.junit.Test;
importstaticorg.hamcrest.CoreMatchers.is;
importstaticorg.junit.Assert.assertThat;
publicclassFactorialZeroCount {
/*
TASK
n!의 결과값에서 0의 개수를 구한다.
*/
@Test
publicvoidtest() {
assertThat(countZero1(getFactorial(5)), is(1));
assertThat(countZero1(getFactorial(12)), is(2));
assertThat(countZero2(5), is(1));
assertThat(countZero2(12), is(2));
}
publicintgetFactorial(intnum) {
intresult = 1;
for (inti = 1; i <= num; i++) {
result *= i;
}
returnresult;
}
publicintcountZero1(intnum) {
intcount = 0;
while (num % 10 == 0) {
num /= 10;
count++;
}
returncount;
}
// 5가 얼마나 곱해졌는지가 중요하다.
publicintcountZero2(intnum) {
intcount = 0;
for (inti = 5; i <= num; i += 5) {
intbase = i;
while (base % 5 == 0) {
base /= 5;
count++;
}
}
returncount;
}
}