-
Notifications
You must be signed in to change notification settings - Fork 2
/
integer-to-roman.java
72 lines (70 loc) · 2.04 KB
/
integer-to-roman.java
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
66
67
68
69
70
71
72
class Solution {
public String intToRoman(int num) {
StringBuilder res = new StringBuilder();
int quotient;
//case num > 1000:
quotient = num / 1000;
num = num - 1000 * quotient;
for (int i = 0; i < quotient; i++)
res.append("M");
//case 900:
quotient = num / 900;
num = num - 900 * quotient;
for (int i = 0; i < quotient; i++)
res.append("CM");
//case 500:
quotient = num / 500;
num -= 500 * quotient;
for (int i = 0; i < quotient; i++)
res.append("D");
//case 400:
quotient = num / 400;
num -= 400 * quotient;
for (int i = 0; i < quotient; i++)
res.append("CD");
//case 100:
quotient = num / 100;
num -= 100 * quotient;
for (int i = 0; i < quotient; i++)
res.append("C");
//case 90:
quotient = num / 90;
num -= 90 * quotient;
for (int i = 0; i < quotient; i++)
res.append("XC");
//case 50:
quotient = num / 50;
num -= 50 * quotient;
for (int i = 0; i < quotient; i++)
res.append("L");
//case 40:
quotient = num / 40;
num -= 40 * quotient;
for (int i = 0; i < quotient; i++)
res.append("XL");
//case 10:
quotient = num / 10;
num -= 10 * quotient;
for (int i = 0; i < quotient; i++)
res.append("X");
//case 9:
quotient = num / 9;
num -= 9 * quotient;
for (int i = 0; i < quotient; i++)
res.append("IX");
//case 5:
quotient = num / 5;
num -= 5 * quotient;
for (int i = 0; i < quotient; i++)
res.append("V");
//case 4:
quotient = num / 4;
num -= 4 * quotient;
for (int i = 0; i < quotient; i++)
res.append("IV");
//case 1:
for (int i = 0; i < num; i++)
res.append("I");
return res.toString();
}
}