当前位置: 代码迷 >> 综合 >> exercism————Roman Numerals
  详细解决方案

exercism————Roman Numerals

热度:33   发布时间:2023-12-13 18:09:39.0

题目:

在这里插入图片描述

解法一:

package exercism;import org.omg.PortableInterceptor.INACTIVE;import java.util.HashMap;
import java.util.Map;/*** 右加左减* 左减数字必须为一位且仅限于Ⅰ、Ⅹ、? ,右加数字不能超过三位* 转换3000以内就可以了*/public class RomanNumerals {
    private String romanNumber;public RomanNumerals(int num) {
    StringBuilder result = new StringBuilder();int digit = (num % 10);int tens = (num % 100 / 10) * 10;int hundreds = (num % 1000 / 100) * 100;int thousands = (num / 1000) * 1000;result.append(getTheThousands(thousands));result.append(getTheHundreds(hundreds));result.append(getTheTens(tens));result.append(getDigit(digit));this.romanNumber = result.toString().replaceAll("null","");}public String getDigit(int digit) {
    Map<Integer,String> digitMap = new HashMap<>(9);digitMap.put(1,"I");digitMap.put(2,"II");digitMap.put(3,"III");digitMap.put(4,"IV");digitMap.put(5,"V");digitMap.put(6,"VI");digitMap.put(7,"VII");digitMap.put(8,"VIII");digitMap.put(9,"IX");return digitMap.get(digit);}public String getTheTens(int tens) {
    Map<Integer,String> tensMap = new HashMap<>(9);tensMap.put(10,"X");tensMap.put(20,"XX");tensMap.put(30,"XXX");tensMap.put(40,"XL");tensMap.put(50,"L");tensMap.put(60,"LX");tensMap.put(70,"LXX");tensMap.put(80,"LXXX");tensMap.put(90,"XC");return tensMap.get(tens);}public String getTheHundreds(int theHundreds) {
    Map<Integer,String> hundredsMap = new HashMap<>();hundredsMap.put(100,"C");hundredsMap.put(200,"CC");hundredsMap.put(300,"CCC");hundredsMap.put(400,"CD");hundredsMap.put(500,"D");hundredsMap.put(600,"DC");hundredsMap.put(700,"DCC");hundredsMap.put(800,"DCCC");hundredsMap.put(900,"CM");return hundredsMap.get(theHundreds);}public String getTheThousands(int theThousands) {
    Map<Integer,String> thousandsMap = new HashMap<>();thousandsMap.put(1000,"M");thousandsMap.put(2000,"MM");return thousandsMap.get(theThousands);}
}

解法二:

import java.util.Map;
import java.util.LinkedHashMap;public class RomanNumeral {
    private int arabicNumeral;private static final Map<Integer, String> conversion = new LinkedHashMap<>();static {
    conversion.put(1000, "M");conversion.put(900, "CM");conversion.put(500, "D");conversion.put(400, "CD");conversion.put(100, "C");conversion.put(90, "XC");conversion.put(50, "L");conversion.put(40, "XL");conversion.put(10, "X");conversion.put(9, "IX");conversion.put(5, "V");conversion.put(4, "IV");conversion.put(1, "I");}public RomanNumeral(int arabicNumeral) {
    this.arabicNumeral = arabicNumeral;}public String getRomanNumeral() {
    String romanNumeral = "";for (int currentValue : conversion.keySet()) {
    while (this.arabicNumeral >= currentValue) {
    romanNumeral += conversion.get(currentValue);this.arabicNumeral -= currentValue;}}return romanNumeral;}
}

解法三:

public class RomanNumeral {
    private final int number;public RomanNumeral(int i) {
    number = i;}String getRomanNumeral() {
    String duizendtallen = new String[]{
    "", "M", "MM", "MMM"}[number / 1000];String honderdtallen = new String[]{
    "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}[(number % 1000) / 100];String tientallen = new String[]{
    "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}[(number % 100) / 10];String eenheden = new String[]{
    "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}[number % 10];return duizendtallen + honderdtallen + tientallen + eenheden;

Conclusion:

  • 解法一: 常规思路, 利用将各个位上的罗马数字用StringBuilder连接
  • 解法二: 功力深厚的写法, 最值得学习!!!
  • 解法三:思路新奇,很简洁,相当于解法一的简化版