SoftwareDo/알고리즘(코테)
leetcode 12. IntegerToRoman (Java)
휴보로
2024. 5. 6. 23:34
https://leetcode.com/problems/integer-to-roman/description/
문제
4000 미만의 int num을 로마 숫자로 변환하라
풀이
public String intToRoman(int num) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < num / 1000; i++) {
sb.append("M");
}
num %= 1000;
if (num / 100 == 9) {
sb.append("CM");
num -= 900;
} else if (num / 100 >= 5) {
sb.append("D");
num -= 500;
} else if (num / 100 == 4) {
sb.append("CD");
num -= 400;
}
for (int i = 0; i < num / 100; i++) {
sb.append("C");
}
num %= 100;
if (num / 10 == 9) {
sb.append("XC");
num -= 90;
} else if (num / 10 >= 5) {
sb.append("L");
num -= 50;
} else if (num / 10 == 4) {
sb.append("XL");
num -= 40;
}
for (int i = 0; i < num / 10; i++) {
sb.append("X");
}
num %= 10;
if (num == 9) {
sb.append("IX");
num = 0;
} else if (num >= 5) {
sb.append("V");
num -= 5;
} else if (num == 4) {
sb.append("IV");
num = 0;
}
for (int i = 0; i < num; i++) {
sb.append("I");
}
return sb.toString();
}
1 = I, 5 = V, 1000 = M인 이상, 이 외에 어떻게 할 수 있을지 모르겠습니다.