forked from shruti170901/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Integer to Roman.cpp
40 lines (40 loc) · 930 Bytes
/
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
class Solution {
public:
void fill(string &ans, int& num, int base, char low, char high, char higher){
if(num>=9*base){
num-=9*base;
ans+=low;
ans+=higher;
}
else if(num>=5*base){
ans+=high;
num-=5*base;
while(num>=base){
ans+=low;
num-=base;
}
}
else if(num>=4*base){
ans+=low;
ans+=high;
num-=4*base;
}
else{
while(num>=base){
ans+=low;
num-=base;
}
}
}
string intToRoman(int num) {
string ans="";
while(num>=1000){
ans+='M';
num-=1000;
}
fill(ans, num, 100, 'C', 'D', 'M');
fill(ans, num, 10, 'X', 'L', 'C');
fill(ans, num, 1, 'I', 'V', 'X');
return ans;
}
};