Leetcode#12 Integer to Roman

原题地址

 

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

 

代码:

 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 }

 

转载于:https://www.cnblogs.com/boring09/p/4253798.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值