-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_INTEGER_TO_ROMAN.cpp
64 lines (57 loc) · 1.23 KB
/
12_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
#include <iostream>
#include <string>
using namespace std;
class Solution
{
public:
string intToRoman(int num)
{
string res;
help(num / 1000, res, 'M', 'P', 'P');
num = num % 1000;
help(num / 100, res, 'C', 'D', 'M');
num = num % 100;
help(num / 10, res, 'X', 'L', 'C');
num = num % 10;
help(num, res, 'I', 'V', 'X');
return res;
}
void help(int digit, string &res, char a, char b, char c)
{
if (digit <= 3)
{
for (int i = 0; i < digit; i++)
{
res.push_back(a);
}
}
else if (digit == 4)
{
res.push_back(a);
res.push_back(b);
}
else if (digit == 5)
{
res.push_back(b);
}
else if (digit > 5 && digit < 9)
{
res.push_back(b);
for (int i = 0; i < digit - 5; i++)
{
res.push_back(a);
}
}
else if (digit == 9)
{
res.push_back(a);
res.push_back(c);
}
}
};
int main()
{
auto res = Solution().intToRoman(1994);
std::cout << res << std::endl;
system("pause");
}