forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegertoRoman.cpp
More file actions
52 lines (52 loc) · 1.26 KB
/
IntegertoRoman.cpp
File metadata and controls
52 lines (52 loc) · 1.26 KB
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
class Solution {
public:
// I, II, III, IV, V, VI, VII, VIII, IX, X.
// I 1
//V 5
//X 10
//L 50
//C 100
//D 500
//M 1,000
string intToRoman(int digit, string one, string five, string ten) {
if(digit==0) {
return "";
}
else if(digit<=3) {
string res = "";
for(int i=0;i<digit;i++){
res.append(one);
}
return res;
}
else if(digit==4) {
return one+five;
}
else if(digit==5) {
return five;
}
else if(digit<=8) {
string res = five;
for(int i=5;i<digit;i++) {
res.append(one);
}
return res;
}
else if(digit==9) {
return one+ten;
}
}
string intToRoman(int num) {
int a1 = num%10;
num /=10;
int a2 = num%10;
num /=10;
int a3 = num%10;
num /= 10;
int a4 = num%10;
return intToRoman(a4, "M", "M", "M")
+ intToRoman(a3, "C", "D", "M")
+ intToRoman(a2, "X", "L", "C")
+ intToRoman(a1, "I", "V", "X");
}
};