1 整数 转 罗马数字
<script>
// I1 V5 X10 L50 C100 D500 M1000
function int2rmn(n){
let s='',b=1;
for(;n!=0;){
let a=n%10*b;
if(a==1){s+='I'}
else if(a==2){s='II'+s}
else if(a==3){s='III'+s}
else if(a==4){s='IV'+s}
else if(a==5){s='V'+s}
else if(a==6){s='VI'+s}
else if(a==7){s='VII'+s}
else if(a==8){s='VIII'+s}
else if(a==9){s='IX'+s}if(a==10){s='X'+s}
n=parseInt(n/10);b=b*10;}
alert(s)}
rmn()
</script>
2 罗马数字转整数
<script>
// I1 V5 X10 L50 C100 D500 M1000
function rmn(s){
let rsu=0;
for(let a=0;a<s.length;a++){
if(a<s.length-1&&s[a]=='I'&&s[a+1]=='V'){rsu+=4;a++}
else if(a<s.length-1&&s[a]=='I'&&s[a+1]=='X'){rsu+=9;a++}
else if(a<s.length-1&&s[a]=='X'&&s[a+1]=='L'){rsu+=40;a++}
else if(a<s.length-1&&s[a]=='X'&&s[a+1]=='C'){rsu+=90;a++}
else if(a<s.length-1&&s[a]=='C'&&s[a+1]=='D'){rsu+=400;a++}
else if(a<s.length-1&&s[a]=='C'&&s[a+1]=='M'){rsu+=900;a++}
else if(s[a]=='I'){rsu++}
else if(s[a]=='V'){rsu+=5}
else if(s[a]=='X'){rsu+=10}
else if(s[a]=='L'){rsu+=50}
else if(s[a]=='C'){rsu+=100}
else if(s[a]=='D'){rsu+=500}
else if(s[a]=='M'){rsu+=1000}
}
alert(rsu)
}
rmn('XCIX')
</script>
1
<script>
// I1 V5 X10 L50 C100 D500 M1000
function rmn(s){
let rsu=0;
for(let a=0;a<s.length;a++){
if(a<s.length-1&&s[a]=='I'&&s[a+1]=='V'){rsu+=4;a++}
else if(a<s.length-1&&s[a]=='I'&&s[a+1]=='X'){rsu+=9;a++}
else if(a<s.length-1&&s[a]=='X'&&s[a+1]=='L'){rsu+=40;a++}
else if(s[a]=='I'){rsu++}
else if(s[a]=='V'){rsu+=5}
else if(s[a]=='X'){rsu+=10}}
alert(rsu)
}
rmn('XL')
</script>