-
Notifications
You must be signed in to change notification settings - Fork 0
/
Integer to Roman.cpp
46 lines (44 loc) · 1.26 KB
/
Integer to Roman.cpp
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
public class Solution {
public String intToRoman(int num) {
// Start typing your Java solution below
// DO NOT write main() function
StringBuffer ans = new StringBuffer();
ans.append(Roman(num / 1000 % 10, '=', '-', 'M'));
ans.append(Roman(num / 100 % 10, 'M', 'D', 'C'));
ans.append(Roman(num / 10 % 10, 'C', 'L', 'X'));
ans.append(Roman(num / 1 % 10, 'X', 'V', 'I'));
return ans.toString();
}
public String Roman(int num, char ten, char five, char one) {
if (num == 9) {
return "" + one + ten;
}
else if (num == 8) {
return "" + five + one + one + one;
}
else if (num == 7) {
return "" + five + one + one;
}
else if (num == 6) {
return "" + five + one;
}
else if (num == 5) {
return "" + five;
}
else if (num == 4) {
return "" + one + five;
}
else if (num == 3) {
return "" + one + one + one;
}
else if (num == 2) {
return "" + one + one;
}
else if (num == 1) {
return "" + one;
}
else {
return "";
}
}
}