-
Notifications
You must be signed in to change notification settings - Fork 139
/
integer_to_roman
92 lines (91 loc) · 1.75 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
82
83
84
85
86
87
88
89
90
91
92
class Solution
{
public:
string intToRoman(int num)
{
string s;
while(num!=0)
{
if (num>=1000)
{
s+='M';
num-=1000;
}
else if (num>=900)
{
s+='C';
s+='M';
num-=900;
}
else if (num>=500)
{
s+='D';
num-=500;
}
else if (num>=400)
{
s+='C';
s+='D';
num-=400;
}
else if (num>=100)
{
s+='C';
num-=100;
}
else if (num>=90)
{
s+='X';
s+='C';
num-=90;
}
else if (num>=50)
{
s+='L';
num-=50;
}
else if (num>=40)
{
s+='X';
s+='L';
num-=40;
}
else if (num>=10)
{
s+='X';
num-=10;
}
else if (num>=9)
{
s+='I';
s+='X';
num-=9;
}
else if (num>=5)
{
s+='V';
num-=5;
}
else if (num>=4)
{
s+='I';
s+='V';
num-=4;
}
else if (num>=1)
{
s+='I';
num-=1;
}
}
return s;
}
};
int main()
{
string s;
Solution object;
s=object.intToRoman(456);
cout<<"In Roman "<<s;
return 0;
}