首页 > 编程知识 正文

Roman Numeral Problem罗马数字问题,罗马大写数字

时间:2023-05-06 06:06:10 阅读:222582 作者:4766

Question One

        Convert number to ttdhb/p>

example:      

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

        If the less number is placed before the larger number,it means substraction,otherwise,it means addition.

Method

        We can list Roman numerals in the ones, tens, hundreds, and thousands places, and then uses the ones, tens, hundreds,and thousands places corresponding to these Roman places to calculate the target value;

/** * List cases where you can combine all numbers. * * @param number * @return */ private static String convertNumbersToNumeral(int number) { if (number > MAX) { return ERROR; } // 千位 String独特的砖头 M = {"", "M", "MM", "MMMM"}; // 百位 String独特的砖头 D = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; // 十位 String独特的砖头 X = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; // 个位 String独特的砖头 I = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; return M[number / 1000] + D[number % 1000 / 100] + X[number % 100 / 10] + I[number % 10]; }

Question Two         On the contrary, how to convert Roman numeral to number?

Method

        Because Roman Numbers are compose of single character, so the target Roman Numbers can be split into a single Roman Number, then we can convert   single Roman Number to Arabic Number. If we get adjancent  two numbers,  the front is less than the current value, we should add the current value, but needs to substract previous number, or directly add the current value.

        The following code is how to convert char to int:

private static int getIntFromChar(char numeral) { switch (numeral) { case 'I': return 1; case 'V': return 5; case 'X': return 10; case 'L': return 50; case 'C': return 100; case 'D': return 500; case 'M': return 1000; default: break; } throw new IllegalArgumentException("illegal number!"); }

        Sum = chartAt(0); When calculate the sum start with the subscript one.

/** * Convert numerals to number */ private static int convertNumeralsToNumber(String numeral) { // 如果前面的字符小于后面一个字符,那么做减法,否则做加法 if (numeral == null || numeral.length() == 0) { return -1; } int sum = getIntFromChar(numeral.charAt(0)); for (int i = 1; i < numeral.length(); i++) { int front = getIntFromChar(numeral.charAt(i - 1)); int current = getIntFromChar(numeral.charAt(i)); if (current > front) { sum += current - front; } else { sum += current; } } return sum; } Complete Code package leetcode100;/** * @author bingbing * @date 2021/8/13 0013 17:32 * 罗马数字问题 * Roman Numeral Problem * I 1 * V 5 * X 10 * L 50 * C 100 * D 500 * M 1000 * IV =4 VI=6 * XL=40 LX=60 * 将数字转换为罗马数字 * Convert numbers to Roman numerals */public class RomanNumeralProblem { private static final int MAX = 3000; private static final String ERROR = "error"; public static void main(String独特的砖头 args) { int a = 2536; String result = convertNumbersToNumeral(a); System.out.println("convert number to numeral, the result is : " + result); int num = convertNumeralsToNumber("VIII"); System.out.println("convert numerals to number ,the result is:" + num); } /** * List cases where you can combine all numbers. * * @param number * @return */ private static String convertNumbersToNumeral(int number) { if (number > MAX) { return ERROR; } // 千位 String独特的砖头 M = {"", "M", "MM", "MMMM"}; // 百位 String独特的砖头 D = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; // 十位 String独特的砖头 X = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; // 个位 String独特的砖头 I = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; return M[number / 1000] + D[number % 1000 / 100] + X[number % 100 / 10] + I[number % 10]; } /** * Convert numerals to number */ private static int convertNumeralsToNumber(String numeral) { // 如果前面的字符小于后面一个字符,那么做减法,否则做加法 if (numeral == null || numeral.length() == 0) { return -1; } int sum = getIntFromChar(numeral.charAt(0)); for (int i = 1; i < numeral.length(); i++) { int front = getIntFromChar(numeral.charAt(i - 1)); int current = getIntFromChar(numeral.charAt(i)); if (current > front) { sum += current - front; } else { sum += current; } } return sum; } private static int getIntFromChar(char numeral) { switch (numeral) { case 'I': return 1; case 'V': return 5; case 'X': return 10; case 'L': return 50; case 'C': return 100; case 'D': return 500; case 'M': return 1000; default: break; } throw new IllegalArgumentException("illegal number!"); }} Test

        Q1:        input: 2536

print result:

Summary

        The above of two kinds of problem are reversible , Arabic numerals to Roman numerals , we can use enumeration and then calculate method . Roman numerals to Arabic, we can use  split  and then conversion  and then calculate method.

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。