forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0012.cpp
More file actions
38 lines (29 loc) · 789 Bytes
/
0012.cpp
File metadata and controls
38 lines (29 loc) · 789 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
30
31
32
33
34
35
36
37
38
class Solution {
public:
string intToRoman(int num) {
map<int, string> map1;
map1[1] = "I";
map1[4] = "IV";
map1[5] = "V";
map1[9] = "IX";
map1[10] = "X";
map1[40] = "XL";
map1[50] = "L";
map1[90] = "XC";
map1[100] = "C";
map1[400] = "CD";
map1[500] = "D";
map1[900] = "CM";
map1[1000] = "M";
string result = "";
while(num > 0){
//reverse iterator
auto it = map1.rbegin();
while(num < it->first)
it++;
num -= it->first;
result += it->second;
}
return result;
}
};