import java.util.Scanner;
public class Integer_to_Romen {
//定义0~9个位数
String[] str_1={"I","II","III","IV","V","VI","VII","VIII","IX"};
//定义10~90整十位数
String[] str_10={"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
//定义100~900整百位数
String[] str_100={"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
//定义1000~3000整千位数
String[] str_1000={"M","MM","MMM"};
//整数转化为罗马数的方法
public void change(int a1){
int b_1000=0;
int b_100=0;
int b_10=0;
int b_1=0;
if(a1>3000||a1<1){
System.out.println("输入的数字有误!!");
System.exit(0);
}else if(a1%1000>=1){
b_1000 = (a1-a1%1000)/1000;
b_100 = ((a1-b_1000*1000)-(a1-b_1000*1000)%100)/100;
b_10 = ((a1-b_1000*1000-b_100*100)-(a1-b_1000*1000-b_100*100)%10)/10;
b_1 = a1-b_1000*1000-b_100*100-b_10*10;
}else if(a1%100>=1){
b_100 = (a1-a1%100)/100;
b_10 = ((a1-b_100*100)-(a1-b_100*100)%10)/10;
b_1 = a1-b_100*100-b_10*10;
}else if(a1%10>=1){
b_10 = (a1-a1%10)/10;
b_1 = a1-b_10*10;
}else{
b_1 = a1;
}
//输出转化以后的罗马数字
if(b_1000>0){
System.out.println(str_1000[b_1000-1].toString()+""+str_100[b_100-1].toString()+""+str_10[b_10-1].toString()+""+str_1[b_1-1].toString());
}else if(b_100>0){
System.out.println(str_100[b_100-1].toString()+""+str_10[b_10-1].toString()+""+str_1[b_1-1].toString());
}else if(b_10>0){
System.out.println(str_10[b_10-1].toString()+""+str_1[b_1-1].toString());
}else{
System.out.println(str_1[b_1-1].toString());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("请输入一个0~3000的数字:");
Scanner c = new Scanner(System.in);
int a = c.nextInt();
Integer_to_Romen ITr = new Integer_to_Romen();
ITr.change(a);
}
}
public class Integer_to_Romen {
//定义0~9个位数
String[] str_1={"I","II","III","IV","V","VI","VII","VIII","IX"};
//定义10~90整十位数
String[] str_10={"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
//定义100~900整百位数
String[] str_100={"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
//定义1000~3000整千位数
String[] str_1000={"M","MM","MMM"};
//整数转化为罗马数的方法
public void change(int a1){
int b_1000=0;
int b_100=0;
int b_10=0;
int b_1=0;
if(a1>3000||a1<1){
System.out.println("输入的数字有误!!");
System.exit(0);
}else if(a1%1000>=1){
b_1000 = (a1-a1%1000)/1000;
b_100 = ((a1-b_1000*1000)-(a1-b_1000*1000)%100)/100;
b_10 = ((a1-b_1000*1000-b_100*100)-(a1-b_1000*1000-b_100*100)%10)/10;
b_1 = a1-b_1000*1000-b_100*100-b_10*10;
}else if(a1%100>=1){
b_100 = (a1-a1%100)/100;
b_10 = ((a1-b_100*100)-(a1-b_100*100)%10)/10;
b_1 = a1-b_100*100-b_10*10;
}else if(a1%10>=1){
b_10 = (a1-a1%10)/10;
b_1 = a1-b_10*10;
}else{
b_1 = a1;
}
//输出转化以后的罗马数字
if(b_1000>0){
System.out.println(str_1000[b_1000-1].toString()+""+str_100[b_100-1].toString()+""+str_10[b_10-1].toString()+""+str_1[b_1-1].toString());
}else if(b_100>0){
System.out.println(str_100[b_100-1].toString()+""+str_10[b_10-1].toString()+""+str_1[b_1-1].toString());
}else if(b_10>0){
System.out.println(str_10[b_10-1].toString()+""+str_1[b_1-1].toString());
}else{
System.out.println(str_1[b_1-1].toString());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("请输入一个0~3000的数字:");
Scanner c = new Scanner(System.in);
int a = c.nextInt();
Integer_to_Romen ITr = new Integer_to_Romen();
ITr.change(a);
}
}