-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegerToRoman.cpp
More file actions
34 lines (34 loc) · 920 Bytes
/
integerToRoman.cpp
File metadata and controls
34 lines (34 loc) · 920 Bytes
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
/*
* Given an integer, convert it to a roman numeral.
* Input is guaranteed to be within the range from 1 to 3999.
*
*/
class Solution {
public:
string intToRoman(int num) {
string unit[9] = {"I","II","III","IV","V","VI","VII","VIII","IX"};
string decade[9] = {"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
string hundred[9] = {"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
string kilo[9] = {"M","MM","MMM"};
string res = "";
int n = num/1000;
if(n>0){
num = num % 1000;
res.append(kilo[n-1]);
}
n = num/100;
if(n>0){
num = num % 100;
res.append(hundred[n-1]);
}
n = num/10;
if(n>0){
num = num % 10;
res.append(decade[n-1]);
}
if(num>0){
res.append(unit[num-1]);
}
return res;
}
};