Roman Numeral Problem(罗马数字问题)
Posted Dream_it_possible!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Roman Numeral Problem(罗马数字问题)相关的知识,希望对你有一定的参考价值。
Question One
Convert number to Roman numeral.
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.
IV =4 VI=6
XL=40 LX=60
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
Q2: input: VIII
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.
以上是关于Roman Numeral Problem(罗马数字问题)的主要内容,如果未能解决你的问题,请参考以下文章
Roman Numeral Converter-freecodecamp算法题目
FCC_Intermediate Algorithm Scripting_Roman Numeral Converter