-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12.cpp
More file actions
29 lines (29 loc) · 756 Bytes
/
12.cpp
File metadata and controls
29 lines (29 loc) · 756 Bytes
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
// ugly.cpp
class Solution {
public:
string intToRoman(int num) {
string res = "";
unordered_map<int, char> hash = {{1, 'I'}, {5, 'V'}, {10, 'X'},
{50, 'L'}, {100, 'C'}, {500, 'D'},
{1000, 'M'}};
for (int base = 1000, r; base; base /= 10) {
r = num / base;
if (r) {
switch (r) {
case 4:
res = res + hash[base] + hash[base * 5];
break;
case 9:
res = res + hash[base] + hash[base * 10];
break;
default:
if (r >= 5)
res.append(1, hash[base * 5]);
res.append(r % 5, hash[base]);
}
num -= r * base;
}
}
return res;
}
};