-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerToRoman.cpp
More file actions
55 lines (53 loc) · 1.43 KB
/
IntegerToRoman.cpp
File metadata and controls
55 lines (53 loc) · 1.43 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
/***
* 法1:按十进制编码
* 定义一个十进制数(个十百千)到罗马数字的映射表
* 从低位到高位逐位转换int
***/
/*
class Solution {
public:
string intToRoman(int num) {
const string i2rMap[4][10] = {
{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
{"", "M", "MM", "MMM", "", "", "", "", "", ""}
};
string sroman;
int d = 0; // digit
while (num)
{
sroman = i2rMap[d++][num % 10] + sroman;
num /= 10;
}
return sroman;
}
};
*/
/***
* 法2:按罗马数字的进制编码
* 4000以下会用到 I V X L C D M 放在大数左边相减的最多只能有一个
* 按照以上进制规律 对 4 9 40等编码
* 从高位往低位计算
***/
class Solution {
public:
string intToRoman(int num) {
const int ints[] = {
1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1
};
const string romans[] = {
"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"
};
string sroman;
int d = 0;
while (num)
{
int n = num / ints[d]; // how many romans in this digit
while (n--)
sroman += romans[d];
num %= ints[d++]; // d++
}
return sroman;
}
};