-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0012_Integer_to_Roman.cpp
More file actions
59 lines (50 loc) · 1.19 KB
/
Copy path0012_Integer_to_Roman.cpp
File metadata and controls
59 lines (50 loc) · 1.19 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
53
54
55
56
57
58
59
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
string intToRoman(int num) {
string ans;
int qian,bai,shi,ge;
qian = num/1000;
bai = num/100%10;
shi = num/10%10;
ge = num%10;
for(int i=0;i<qian;i++){ ans+="M"; }
if(bai<=3){
for(int i=0;i<bai;i++) ans+="C";
}
else if(bai==4){ ans+="CD"; }
else if(bai<=8){
ans+="D";
for(int i=5;i<bai;i++) ans+="C";
}
else{ ans+="CM"; }
if(shi<=3){
for(int i=0;i<shi;i++) ans+="X";
}
else if(shi==4){ ans+="XL"; }
else if(shi<=8){
ans+="L";
for(int i=5;i<shi;i++) ans+="X";
}
else{ ans+="XC"; }
if(ge<=3){
for(int i=0;i<ge;i++) ans+="I";
}
else if(ge==4){ ans+="IV"; }
else if(ge<=8){
ans+="V";
for(int i=5;i<ge;i++) ans+="I";
}
else{ ans+="IX"; }
return ans;
}
};
int main(){
Solution solve;
int x = 1994;
cout << "Input: " << x << endl;
cout << "Output: " << solve.intToRoman(x) << endl;
return 0;
}