https://leetcode.com/problems/integer-to-roman/

原题:

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

 

思路:

掌握罗马数字规则并实现即可。(具体规则见 LeetCode(13):Roman to Integer)

 

我的AC代码:

 1 class Solution {
 2 public:
 3     string intToRoman(int num) {
 4         string in[] = {"I","V","X","L","C","D","M"};
 5         int t,i=0,j=0;
 6         string res="",tmp="";
 7        // cout<<(tmp += in[0]+in[1]);
 8         while(num>0){
 9             t=num%10;
10             switch (t){
11                 case 0: break;
12                 case 1: tmp = in[i]; break;
13                 case 2: tmp = in[i]+in[i]; break; 
14                 case 3: tmp = in[i]+in[i]+in[i]; break;
15                 case 4: tmp = in[i]+in[i+1]; break;
16                 case 5: tmp = in[i+1]; break;
17                 case 6: tmp = in[i+1]+in[i]; break;
18                 case 7: tmp = in[i+1]+in[i]+in[i]; break;
19                 case 8: tmp = in[i+1]+in[i]+in[i]+in[i]; break;
20                 case 9: tmp = in[i]+in[i+2]; break;
21             }
22             i += 2;
23        //     cout<<in[1]+in[0]+in[0]+in[0]<<"  "<<tmp<<endl;
24             res = tmp +res;
25             num=num/10;
26             tmp="";
27             }
28             cout<< res;
29         return res;
30     }
31 };