-
Notifications
You must be signed in to change notification settings - Fork 0
/
integer_to_roman.cpp
66 lines (65 loc) · 1.44 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
class Solution {
public:
string arabicToRoman(int i) {
switch(i) {
case 1000: {
return "M";
}
case 900: {
return "CM";
}
case 500:{
return "D";
}
case 400:{
return "CD";
}
case 100:{
return "C";
}
case 90:{
return "XC";
}
case 50:{
return "L";
}
case 40:{
return "XL";
}
case 10:{
return "X";
}
case 9:{
return "IX";
}
case 5:{
return "V";
}
case 4:{
return "IV";
}
case 1:{
return "I";
}
}
return "";
}
string intToRoman(int num) {
int values[] = {1000, 900,500,400,100,90,50,40,10,9,5,4,1};
vector<int> d;
int n = num, k = 0;
while(num) {
if (num - values[k] >= 0) {
num -= values[k];
d.push_back(values[k]);
} else {
++k;
}
}
string answer = "";
for (int i = 0; i < d.size(); ++i) {
answer += arabicToRoman(d[i]);
}
return answer;
}
};