Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
想法
类似于取出一个数字各个位置上的数,例如321取出 3、2、1构成的罗马数字为CCC XX I
JAVA代码
public class Solution {
public String intToRoman(int num){
String a [][] = {
{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
{"", "M", "MM", "MMM"}
};
String res = "";
int k = 0;
while (num != 0){
int t = num % 10;
res = a[k][t] + res;
k++;
num = num / 10;
}
return res;
}
}