赞
踩
罗马数字包含以下七种字符: I
, V
, X
, L
,C
,D
和 M
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
这道题对应的是leetcode 中的12.整数转罗马数字
- package example;
-
- public class AlaboJiSuan {
-
-
- //组合数据左减右加
- String[] thousands = {"", "M", "MM", "MMM"};//1000,2000,3000
- String[] hundreds = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; //100,200,300,400,500,600,700,800.900
- String[] tens = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; //10,20,30,40,50,60,70,80,90
- String[] ones = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};//123456789
-
- public static void main(String[] args) {
-
- int remark = 11;
-
-
-
-
- AlaboJiSuan alaboJiSuan = new AlaboJiSuan();
-
- String numberStr = alaboJiSuan.getAlboNumberStr(remark);
-
- System.out.println(numberStr);
-
- System.out.println(alaboJiSuan.getAlboNumberStr(3999));
- }
-
-
- // 11
- // 11/%10
- private String getAlboNumberStr(int remark) {
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append(thousands[remark/1000]);
- stringBuilder.append(hundreds[remark%1000/100]);
- stringBuilder.append(tens[remark%100/10]);
- stringBuilder.append(ones[remark%10]);
- return stringBuilder.toString();
- }
- }
解决问题的思路其实是转换 0-9 转换成罗马数字的个十百千位数据。基本方法还是获取到每一位的数字。
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。