-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_int_to_roman.cc
46 lines (43 loc) · 1015 Bytes
/
12_int_to_roman.cc
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
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <iostream>
#include <tuple>
#include <queue>
#include <stack>
using namespace::std;
class Solution {
public:
string intToRoman(int num) {
vector<string> base{"IVX", "XLC", "CDM", "M"};
int cnt = 0;
stack<string> stk;
while (num) {
int val = num % 10;
if (val < 4)
stk.push(string (val, base[cnt][0]));
else if (val == 4)
stk.push({base[cnt][0], base[cnt][1]});
else if (val < 9) {
string s = {base[cnt][1]};
stk.push(s + string (val - 5, base[cnt][0]));
}
else if (val == 9)
stk.push({base[cnt][1], base[cnt][2]});
num /= 10;
++cnt;
}
string res;
while (!stk.empty()) {
res += stk.top();
stk.pop();
}
return res;
}
};
int main()
{
return 0;
}