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

LeetCode – Refresh – Integer to Roman

时间:2015-03-20 06:53:42      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

Pretty straight forward.

 1 class Solution {
 2 public:
 3     string getRoman(int n, char ten, char five, char one) {
 4         string result;
 5         if (n == 9) {
 6             result += one;
 7             result += ten;
 8         } else if (n >= 5) {
 9             result += five;
10             while (n-- > 5) result += one;
11         } else if (n == 4) {
12             result += one;
13             result += five;
14         } else {
15             while (n-- > 0) result += one;
16         }
17         return result;
18     }
19     string intToRoman(int num) {
20         string result;
21         result = getRoman(num/1000%10, 0, 0, M);
22         result += getRoman(num/100%10, M, D, C);
23         result += getRoman(num/10%10, C, L, X);
24         result += getRoman(num%10, X, V, I);
25         return result;
26     }
27 };

 

LeetCode – Refresh – Integer to Roman

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4352612.html

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