-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12. Integer to Roman
70 lines (67 loc) · 1.73 KB
/
12. Integer to Roman
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
class Solution {
public:
string intToRoman(int num) {
string result;
while(num){
while(num >= 1000){
result += "M";
num -= 1000;
}
while(num >= 500){
if(num >= 900){
result += "CM";
num -= 900;
break;
}
result += "D";
num -= 500;
}
while(num >= 100){
if(num >= 400){
result += "CD";
num -= 400;
break;
}
result += "C";
num -= 100;
}
while(num >= 50){
if(num >= 90){
result += "XC";
num -= 90;
break;
}
result += "L";
num -= 50;
}
while(num >= 10){
if(num >= 40){
result += "XL";
num -= 40;
break;
}
result += "X";
num -= 10;
}
while(num >= 5){
if(num >= 9){
result += "IX";
num -= 9;
break;
}
result += "V";
num -= 5;
}
while(num >= 1){
if(num >= 4){
result += "IV";
num -= 4;
break;
}
result += "I";
num -= 1;
}
}
return result;
}
};