-
Notifications
You must be signed in to change notification settings - Fork 44
/
Integer to Roman
81 lines (78 loc) · 1.36 KB
/
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
71
72
73
74
75
76
77
78
79
80
81
class Solution {
public:
string intToRoman(int num) {
string s;
int a=num%10;
num/=10;
int b=num%10;
b*=10;
num/=10;
int c=num%10;
c*=100;
num/=10;
int d=num%10;
d*=1000;
num/=10;
while(d>0)
{
s+='M';
d-=1000;
}
if(c==400)
s+="CD";
else if(c==900)
s+="CM";
else
{
if(c>=500)
{
s+='D';
c-=500;
}
while(c>0)
{
s+='C';
c-=100;
}
}
if(b==40)
s+="XL";
else if(b==90)
s+="XC";
else
{
if(b>=50)
{
s+='L';
b-=50;
}
while(b>0)
{
s+='X';
b-=10;
}
}
if(a==4)
{
s+="IV";
}
else if(a==9)
{
s+="IX";
}
else
{
if(a>=5)
{
s+='V';
a-=5;
}
while(a>0)
{
s+='I';
a--;
}
}
return s;
}
};