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

Leetcode#12 Integer to Roman

时间:2015-01-27 20:16:36      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

原题地址

 

找规律+模拟,真没啥可说的。

 

代码:

 1 string intToRoman(int num) {
 2         string res;
 3         
 4         while (num >= 1000) {
 5             res += "M";
 6             num -= 1000;
 7         }
 8         if (num >= 900) {
 9             res += "CM";
10             num -= 900;
11         }
12         if (num >= 500) {
13             res += "D";
14             num -= 500;
15         }
16         if (num >= 400) {
17             res += "CD";
18             num -= 400;
19         }
20         while (num >= 100) {
21             res += "C";
22             num -= 100;
23         }
24         if (num >= 90) {
25             res += "XC";
26             num -= 90;
27         }
28         if (num >= 50) {
29             res += "L";
30             num -= 50;
31         }
32         if (num >= 40) {
33             res += "XL";
34             num -= 40;
35         }
36         while (num >= 10) {
37             res += "X";
38             num -= 10;
39         }
40         if (num >= 9) {
41             res += "IX";
42             num -= 9;
43         }
44         if (num >= 5) {
45             res += "V";
46             num -= 5;
47         }
48         if (num >= 4) {
49             res += "IV";
50             num -= 4;
51         }
52         while (num >= 1) {
53             res += "I";
54             num--;
55         }
56         
57         return res;
58 }

 

Leetcode#12 Integer to Roman

标签:

原文地址:http://www.cnblogs.com/boring09/p/4253798.html

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