1 class Solution {
2 public:
3 string intToRoman(
int num) {
4 const static char* Roman =
"IVXLCDM";
5 string ret;
6 for (
int base =
0; num; num /=
10,
base +=
2) {
7 int x = num %
10;
8 switch (x) {
9 case 1:
case 2:
case 3:
10 ret =
string(x, Roman[
base]) +
ret;
11 break;
12 case 4:
13 ret = Roman[
base+
1] +
ret;
14 ret = Roman[
base] +
ret;
15 break;
16 case 5:
17 ret = Roman[
base+
1] +
ret;
18 break;
19 case 6:
case 7:
case 8:
20 ret =
string(x-
5, Roman[
base]) +
ret;
21 ret = Roman[
base+
1] +
ret;
22 break;
23 case 9:
24 ret = Roman[
base+
2] +
ret;
25 ret = Roman[
base] +
ret;
26 break;
27 default:
28 break;
29 }
30 }
31 return ret;
32 }
33 };
转载于:https://www.cnblogs.com/tanghulu321/archive/2013/05/13/3074956.html
相关资源:数据结构—成绩单生成器
转载请注明原文地址: https://win8.8miu.com/read-1553552.html