我知道寫得很爛.....但我盡力寫了>< 也終於讓答案對了 可是卻出錯QAQ
請求高手們幫忙一下!
import java.util.*;
public class format
{
static int ConvertToNum(String str) //把As轉為數字
{
int num = 0;
switch (str)
{
case "I":
{
num = 1;
break;
}
case "V":
{
num = 5;
break;
}
case "X":
{
num = 10;
break;
}
case "L":
{
num = 50;
break;
}
case "C":
{
num = 100;
break;
}
case "D":
{
num = 500;
break;
}
case "M":
{
num = 1000;
break;
}
}
return num;
}
static int Doconvert(String A)
{
String[] As = A.split(""); //分割輸入(仍是文字)
int convAs[] = new int[As.length]; //用來存As轉換後的數字
int convnum=0; //用來計算convAs的總和
for (int i = 0; i < As.length; i++) //把As轉為數字
{
convAs[i] = ConvertToNum(As[i]);
if(i>0&&convAs[i]>convAs[i-1])
{
convAs[i-1]=-1*convAs[i-1];
}
}
for(int i=0;i<As.length;i++) //把數字轉為羅馬
{
convnum+=convAs[i];
}
return convnum;
}
static String ConvertToRoma(int num)
{
String rnum[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; //儲存所有羅馬數字}
int anum[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
String Output="";
for(int i=0;num>0;i++)
{
while(num>=anum[i])
{
Output+=rnum[i];
num-=anum[i];
}
}
return Output;
}
public static void main(String[] args)
{
Scanner scn = new Scanner(System.in);
while (true)
{
String A = scn.next();
String B = scn.next();
int num = 0;
String[] As = A.split(""); //分割輸入(仍是文字)
int convAs[] = new int[As.length]; //用來存As轉換後的數字
int a=0;//用來計算convAs的總和
int b=0;
int minus=0;
if(A.equals("#"))
{
break;
}
else if(A.equals(B))
{
System.out.println("ZERO");
}
else
{
a=Doconvert(A);
b=Doconvert(B);
minus=a-b;
if(minus<0)
{
minus=-minus;
}
System.out.println(ConvertToRoma(minus));
}
}
}
}