forked from iphkwan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Integer_to_Roman.cc
33 lines (33 loc) · 1.03 KB
/
Integer_to_Roman.cc
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
class Solution {
public:
string intToRoman(int num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string symbol = "IVXLCDM", ret = "";
int div = 1000;
for (int i = 6; i >= 0; i -= 2) {
int tmp = num / div;
if (tmp == 0) {
div /= 10;
continue;
}
if (tmp <= 3) {
ret.append(tmp, symbol[i]);
} else if (tmp == 4) {
ret.append(1, symbol[i]);
ret.append(1, symbol[i + 1]);
} else if (tmp == 5) {
ret.append(1, symbol[i + 1]);
} else if (tmp <= 8) {
ret.append(1, symbol[i + 1]);
ret.append(tmp - 5, symbol[i]);
} else {
ret.append(1, symbol[i]);
ret.append(1, symbol[i + 2]);
}
num = num % div;
div = div / 10;
}
return ret;
}
};