码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode(12)--Integer to Roman

时间:2015-06-03 15:06:51      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:

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

 

LeetCode(12)--Integer to Roman

标签:

原文地址:http://www.cnblogs.com/aezero/p/4548875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!