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 };

 

posted on 2013-05-13 01:04  tanghulu321  阅读(94)  评论(0编辑  收藏  举报