-
Notifications
You must be signed in to change notification settings - Fork 7
/
integertoroman.cpp
70 lines (65 loc) · 1.3 KB
/
integertoroman.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
65
66
67
68
69
70
string getVal(int n,int place){
string one,five,ten;
string res;
if(place==1){
one="I";
five="V";
ten="X";
}
else if(place==10){
one="X";
five="L";
ten="C";
}
else if(place==100){
one="C";
five="D";
ten="M";
}
else if(place==1000){
one="M";
five="M";
ten="M";
}
if(n==5){
return five;
}
else if(n==0){
return "";
}
else if(n==9){
return ten+one;
}
else if(n==4){
return five+one;
}
else if(n<4){
for(int i=0;i<n;++i){
res+=one;
}
return res;
}
else if(n>5){
for(int i=5;i<n;++i){
res+=one;
}
res+=five;
return res;
}
}
string Solution::intToRoman(int A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
int x =A;
string res;
int dec=1;
while(x){
res+= getVal(x%10,dec);
dec*=10;
x/=10;
}
reverse(res.begin(),res.end());
return res;
}