-
Notifications
You must be signed in to change notification settings - Fork 0
/
Integer to Roman.cpp
76 lines (72 loc) · 1.97 KB
/
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
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
class Solution {
public:
string intToRoman(int num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string str = "";
int tmp = num / 1000;
str += intToRomanByBit(tmp,1000);
num = num % 1000;
tmp = num / 100;
str += intToRomanByBit(tmp,100);
num = num % 100;
tmp = num / 10;
str += intToRomanByBit(tmp,10);
tmp = num % 10;
str += intToRomanByBit(tmp,1);
return str;
}
// bit can be 1,10,100,1000
string intToRomanByBit(int num, int bit)
{
switch(bit)
{
case 1:
return intToRomanByBit(num, "I","V","X");
case 10:
return intToRomanByBit(num, "X","L","C");
case 100:
return intToRomanByBit(num, "C","D","M");
case 1000:
return intToRomanByBit(num, "M","M","M");
}
return "";
}
string intToRomanByBit(int num, string oneChar, string fiveChar, string tenChar)
{
string str = "";
switch(num)
{
case 0:
break;
case 1:
str = oneChar;
break;
case 2:
str = oneChar + oneChar;
break;
case 3:
str = oneChar + oneChar + oneChar;
break;
case 4:
str = oneChar + fiveChar;
break;
case 5:
str = fiveChar;
break;
case 6:
str = fiveChar + oneChar;
break;
case 7:
str = fiveChar + oneChar + oneChar;
break;
case 8:
str = fiveChar + oneChar + oneChar + oneChar;
break;
case 9:
str = oneChar + tenChar;
break;
}
return str;
}
};