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
55
56
57
58
59
60
61
62
63
64
65
|
import java.util.HashMap;
import java.util.Map;
public class Problem017 {
public static void main(String[] args) {
System.out.println(run());
}
public static String run() {
Map<Integer, Integer> map = new HashMap<>();
int result = "onethousand".length();
int count = 1;
String[] numbers1 = {
"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"
};
String[] numbers2 = {
"twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"
};
map.put(0, "".length());
for(int i = 0; i < numbers1.length; i++) { map.put(i + 1, numbers1[i].length()); }
for(int i = 0; i < numbers2.length; i++) { map.put((i * 10) + 20, numbers2[i].length()); }
map.put(100, "hundredand".length());
while(count < 20) {
result += map.get(count);
count++;
}
while(count < 100) {
result += map.get((count/10)*10);
result += map.get(count%10);
count++;
}
while(count <= 999) {
if(count%100 < 20) {
result += map.get(count/100);
result += map.get(100);
result += map.get(count%100);
} else {
result += map.get(count/100);
result += map.get(100);
result += map.get(((count%100)/10)*10);
result += map.get(count%10);
}
count++;
}
count = 100;
while(count <= 900) {
result -= "and".length();
count += 100;
}
return Integer.toString(result);
}
}
|
cs |
정답: 21124
실행 시간: 1.5ms = 0.0015초
단어들을 각각 맞는 숫자를 키로, 단어의 길이를 값으로 매핑해 HashMap 컬렉션에 저장하고, 1~1000까지의 숫자로 길이를 읽어와 result 에 더한다.
'Programming > Solutions' 카테고리의 다른 글
[BAEKJOON] 2581번 (0) | 2019.05.04 |
---|---|
[BAEKJOON] 1978번 (0) | 2019.05.02 |
[Project Euler] Problem 016 (0) | 2019.04.16 |
[Project Euler] Problem 015 (0) | 2019.04.16 |
[Project Euler] Problem 014 (0) | 2019.04.16 |