-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode12.cpp
73 lines (73 loc) · 1.21 KB
/
leetcode12.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Solution {
public:
string intToRoman(int num) {
string ret = "";
int tmp = num;
while(tmp >= 1000)
{
ret += "M";
tmp -= 1000;
}
if(tmp >= 900)
{
ret += "CM";
tmp -= 900;
}
if(tmp >= 500)
{
ret += "D";
tmp -= 500;
}
if(tmp >= 400)
{
ret += "CD";
tmp -= 400;
}
while(tmp >= 100)
{
ret += "C";
tmp -= 100;
}
if(tmp >= 90)
{
ret += "XC";
tmp -= 90;
}
if(tmp >= 50)
{
ret += "L";
tmp -= 50;
}
if(tmp >= 40)
{
ret += "XL";
tmp -= 40;
}
while(tmp >= 10)
{
ret += "X";
tmp -= 10;
}
if(tmp >= 9)
{
ret += "IX";
tmp -= 9;
}
if(tmp >= 5)
{
ret += "V";
tmp -= 5;
}
if(tmp >= 4)
{
ret += "IV";
tmp -= 4;
}
while(tmp >= 1)
{
ret += "I";
tmp -= 1;
}
return ret;
}
};