Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
and M
.
Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, two is written as II
in Roman numeral, just two one's added together. Twelve is written as, XII
, which is simply X
+ II
. The number twenty seven is written as XXVII
, which is XX
+ V
+ II
.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII
. Instead, the number four is written as IV
. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX
. There are six instances where subtraction is used:
I
can be placed beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can be placed beforeD
(500) andM
(1000) to make 400 and 900.
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
Example 1:
Input: 3 Output: "III"
Example 2:
Input: 4 Output: "IV"
Example 3:
Input: 9 Output: "IX"
Example 4:
Input: 58 Output: "LVIII" Explanation: C = 100, L = 50, XXX = 30 and III = 3.
Example 5:
Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
大体就是把数字转换为罗马字母,我这里用javascript写的,第一种方法用的是遍历;第二种主要用的是条件判断;第三种方法最简单了,用时最少,是参考leetcode上一个大佬写的。
第一种方法:
var intToRoman = function(num) {
var str=''+num;
//声明一个二维数组
var romanArr=[['I','V'],['X','L'],['C','D'],['M']];
var toRoman='';
var roman=[''];
//传入的num分别取出个、十、百、千四个数存入数组
var numberArr=[num%10,Math.floor(num/10)%10,Math.floor(num/100)%10,Math.floor(num/1000)];
for(var i=0;i<str.length;i++){
if(numberArr[i]>=1&&numberArr[i]<4){
roman[i]=romanArr[i][0];
for(var j=1;j<numberArr[i];j++){
roman[i]+=romanArr[i][0];
}
}
if(numberArr[i]>=5&&numberArr[i]<=8){
roman[i]=romanArr[i][1];
for(var m=5;m<numberArr[i];m++){
roman[i]+=romanArr[i][0];
}
}
if(numberArr[i]==4){
roman[i]=romanArr[i][0]+romanArr[i][1];
}
if(numberArr[i]==9){
roman[i]=romanArr[i][0]+romanArr[i+1][0];
}
}
for(var m=roman.length-1;m>=0;m--){
if(roman[m]==undefined){
roman[m]='';
}
toRoman+=roman[m];
}
return toRoman;
}
第二种方法:(代码较多)
var intToRoman = function(num) {
var str=''+num;
//var roman;
var roman1='',roman2='',roman3='',roman4='';
var thousand=Math.floor(num/1000),
hundred=Math.floor(num/100)%10,
ten=Math.floor(num/10)%10,
individual=num%10;
if(individual>=1&&individual<4){
roman1 = 'I';
for(var i=1;i<individual;i++){
roman1+='I';
}
}
if(individual>=5&&individual<=8){
roman1 = 'V';
for(var j=5;j<individual;j++){
roman1+='I';
}
}
if(individual==4){
roman1='IV';
}
if(individual==9){
roman1='IX';
}
if(ten>=1&&ten<4){
roman2 ='X';
for(var i=1;i<ten;i++){
roman2+='X';
}
}
if(ten>=5&&ten<=8){
roman2 = 'L';
for(var j=5;j<ten;j++){
roman2+='X';
}
}
if(ten==4){
roman2='XL';
//return roman2;
}
if(ten==9){
roman2='XC';
}
if(hundred>=1&&hundred<4){
roman3 ='C';
for(var i=1;i<hundred;i++){
roman3+='C';
}
}
if(hundred>=5&&hundred<=8){
roman3 = 'D';
for(var j=5;j<hundred;j++){
roman3+='C';
}
}
if(hundred==4){
roman3='CD';
}
if(hundred==9){
roman3='CM';
}
if(thousand>=1&&thousand<4){
roman4 ='M';
for(var i=1;i<thousand;i++){
roman4+='M';
}
}
if(str.length==1){return roman1;}
if(str.length==2){
return roman2+roman1;
}
if(str.length==3){return roman3+roman2+roman1;}
if(str.length==4){return roman4+roman3+roman2+roman1;}
};
第三种方法:
var intToRoman = function(num) {
var M=["", "M", "MM", "MMM"];
var C=["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"];
var X=["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"];
var I= ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
大佬代码如下:
String M[] = {"", "M", "MM", "MMM"};
String C[] = {"", "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[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
};