-
Notifications
You must be signed in to change notification settings - Fork 0
/
12-Integer to Roman.cpp
51 lines (51 loc) · 1.11 KB
/
12-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
47
48
49
50
51
class Solution {
public:
string intToRoman(int num)
{
string result="";
result.reserve(100);
int count;
count=num/1000;
if(count>0)
{
num%=count*1000;
while(count--)
result+='M';
}
if(num>=900)
result+="CM",num%=900;
if(num>=500)
result+='D',num%=500;
if(num>=400)
result+="CD",num%=400;
count=num/100;
if(count>0)
{
num%=count*100;
while(count--)
result+='C';
}
if(num>=90)
result+="XC",num%=90;
if(num>=50)
result+='L',num%=50;
if(num>=40)
result+="XL",num%=40;
count=num/10;
if(count>0)
{
num%=count*10;
while(count--)
result+='X';
}
if(num==9)
result+="IX",num=0;
if(num>=5)
result+='V',num%=5;
if(num==4)
result+="IV",num=0;
while(num--)
result+='I';
return result;
}
};