首页 > 其他 > 详细

LeetCode – Refresh – Integer to Roman

时间:2015-03-20 06:50:33      阅读:211      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!