# 整数转罗马数字Java版（力扣）

21 篇文章 9 订阅

I 1
V 5
X 10
L 50
C 100
D 500
M 1000

I 可以放在 V (5) 和 X (10) 的左边，来表示 4 和 9。
X 可以放在 L (50) 和 C (100) 的左边，来表示 40 和 90。
C 可以放在 D (500) 和 M (1000) 的左边，来表示 400 和 900。

1 <= num <= 3999

class Solution {
public String intToRoman(int num) {

String []roman = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int []nums = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};

StringBuilder res = new StringBuilder();

for(int i=0;i<nums.length&&num>=0;i++){
while(nums[i]<=num){
num-=nums[i];
res.append(roman[i]);
}
}
return res.toString();
}
}

package com.Keafmd.April.day14;

/**
* Keafmd
*
* @ClassName: IntegerToRoman
* @Description: 整数转罗马数字 https://leetcode-cn.com/problems/integer-to-roman/
* @author: 牛哄哄的柯南
* @Date: 2021-04-14 9:37
* @Blog: https://keafmd.blog.csdn.net/
*/
public class IntegerToRoman {
public static void main(String[] args) {

Solution0414 solution0414 = new Solution0414();
int num= 1994;
String result = solution0414.intToRoman(num);
System.out.println("result = " + result);

}
}

class Solution0414 {
public String intToRoman(int num) {

String []roman = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int []nums = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};

StringBuilder res = new StringBuilder();

for(int i=0;i<nums.length&&num>=0;i++){
while(nums[i]<=num){
num-=nums[i];
res.append(roman[i]);
}
}
return res.toString();
}
}

result = MCMXCIV

Process finished with exit code 0

Keafmd

• 31
点赞
• 8
收藏
• 打赏
• 3
评论
08-11 110
09-22 418
09-04 88
02-28 118
12-27 50
09-17 139
09-14 352
08-05 182
04-20 1822
11-12 78
07-21 110

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

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