
-Code
import java.io.*;
import java.util.*;
public class BOJ6769 {
static Map<Character, Integer> symbolValue = new HashMap<>() {{
put('I', 1);
put('V', 5);
put('X', 10);
put('X', 10);
put('L', 50);
put('C', 100);
put('D', 500);
put('M', 1000);
}};
static class AromaticInfo {
int roma, val;
public AromaticInfo(int roma, int val) {
this.roma = roma;
this.val = val;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
String aromaticNumber = br.readLine();
ArrayList<AromaticInfo> aromaticList = new ArrayList<>();
for (int i = 0; i < aromaticNumber.length(); i += 2) {
int val = calc(aromaticNumber.substring(i, i + 2));
AromaticInfo info = new AromaticInfo(
symbolValue.get(aromaticNumber.charAt(i + 1 )),
val
);
aromaticList.add(info);
}
int answer = 0;
for (int i = 0; i < aromaticList.size() - 1; i++) {
if (aromaticList.get(i).roma < aromaticList.get(i + 1).roma) {
answer -= aromaticList.get(i).val;
} else {
answer += aromaticList.get(i).val;
}
}
// 마지막은 다음 수가 없으니 무조건 더하기
System.out.println(answer + aromaticList.get(aromaticList.size() - 1).val);
}
private static int calc(String now) {
int arabia = now.charAt(0) - '0';
int roma = symbolValue.get(now.charAt(1));
return arabia * roma;
}
}
처음에는 앞값과 비교를 안 해도 되는 걸로 잘못 이해해서 틀렸었고 마지막에 값을 까먹고 있어서 한 번 더 틀렸었습니다.
'Beakjoon&프로그래머스 > Java' 카테고리의 다른 글
| [프로그래머스/Java] 아이템 줍기 (0) | 2026.01.10 |
|---|---|
| [백준/Java] 1149번 RGB거리 (0) | 2026.01.10 |
| [백준/Java] 1793번 타일링 (0) | 2026.01.09 |
| [백준/Java] 2502번 떡 먹는 호랑이 (0) | 2026.01.09 |
| [백준/Java] 11727번 2×n 타일링 2 (0) | 2026.01.09 |