a013.
羅馬數字
--
NPSC 模擬試題
| From: [114.33.50.204] |
發表日期
:
2012-11-22 03:15
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i,sum,sum1,sum2;
char num1[15],num2[15],n1[10][5]={"","I","II","III","IV","V","VI","VII","VIII","IX"},n2[10][5]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},n3[10][5]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},n4[4][4]={"","M","MM","MMM"};
while(scanf("%s",&num1)&&num1[0]!='#')
{
scanf("%s",&num2);
sum=0,sum1=0,sum2=0;
for(i=0;i<strlen(num1);i++)
{
switch(num1[i])
{
case 'I':
if(num1[i+1]=='V')
sum1+=4,i++;
else if(num1[i+1]=='X')
sum1+=9,i++;
else
sum1+=1;
break;
case 'V':
sum1+=5;
break;
case 'X':
if(num1[i+1]=='L')
sum1+=40,i++;
else if(num1[i+1]=='C')
sum1+=90,i++;
else
sum1+=10;
break;
case 'L':
sum1+=50;
break;
case 'C':
if(num1[i+1]=='D')
sum1+=400,i++;
else if(num1[i+1]=='M')
sum1+=900,i++;
else
sum1+=100;
break;
case 'D':
sum1+=500;
break;
case 'M':
sum1+=1000;
break;
}
}
for(i=0;i<strlen(num2);i++)
{
switch(num2[i])
{
case 'I':
if(num2[i+1]=='V')
sum2+=4,i++;
else if(num2[i+1]=='X')
sum2+=9,i++;
else
sum2+=1;
break;
case 'V':
sum2+=5;
break;
case 'X':
if(num2[i+1]=='L')
sum2+=40,i++;
else if(num2[i+1]=='C')
sum2+=90,i++;
else
sum2+=10;
break;
case 'L':
sum2+=50;
break;
case 'C':
if(num2[i+1]=='D')
sum2+=400,i++;
else if(num2[i+1]=='M')
sum2+=900,i++;
else
sum2+=100;
break;
case 'D':
sum2+=500;
break;
case 'M':
sum2+=1000;
break;
}
}
sum=(sum1>sum2?sum1-sum2:sum2-sum1);
printf(!sum?"ZERO\n":"%s%s%s%s\n",n4[sum/1000],n3[(sum%1000)/100],n2[(sum%100)/10],n1[(sum%10)]);
}
return 0;
}