-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathsolution.cpp
41 lines (40 loc) · 939 Bytes
/
solution.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
class Solution
{
public:
void romanstr(string& roman, int num, char* symbol)
{
if(num == 0)return;
else if(num <= 3) roman.append(num, *symbol);
else if(num == 4)
{
roman.append(1,*symbol);
roman.append(1,*(symbol+1));
}
else if(num <= 8)
{
roman.append(1,*(symbol+1));
roman.append(num-5,*symbol);
}
else if(num == 9)
{
roman.append(1,*symbol);
roman.append(1,*(symbol+2));
}
}
string intToRoman(int num)
{
char symbol[9] = {'I','V','X','L','C','D','M'};
string roman="";
int scale = 1000;
int p=6;
while(num)
{
int bit = num/scale;
romanstr(roman, bit, symbol+p);
num = num%scale;
scale /= 10;
p -= 2;
}
return roman;
}
};