a013.
羅馬數字
--
NPSC 模擬試題
| From: [1.175.78.117] |
發表日期
:
2015-06-14 22:50
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char a[10000],b[10000];
int i,j;
int c[13]={1,4,5,9,10,40,50,90,100,400,500,900,1000};
while(scanf("%s %s",a,b))
{
if(a[0]=='#'||b[0]=='#') break;
int total,total_a=0,total_b=0,str_a,str_b;
str_a=strlen(a);str_b=strlen(b);
for(i=0;i<str_a;i++)
switch(a[i])
{
case 'I':total_a+=1; break;
case 'V':total_a+=5; break;
case 'X':total_a+=10; break;
case 'L':total_a+=50; break;
case 'C':total_a+=100; break;
case 'D':total_a+=500; break;
case 'M':total_a+=1000; break;
}
for(i=0;i<str_b;i++)
switch(b[i])
{
case 'I':total_b+=1; break;
case 'V':total_b+=5; break;
case 'X':total_b+=10; break;
case 'L':total_b+=50; break;
case 'C':total_b+=100; break;
case 'D':total_b+=500; break;
case 'M':total_b+=1000; break;
}
total=total_a-total_b;
if(total<0) total*=-1;
if(total==0) printf("ZERO");
while(total!=0)
{
for(i=12;i>=0;i--)
{
while(total>=c[i])
{
total-=c[i];
switch(i)
{
case 0:printf("I"); break;
case 1:printf("IV"); break;
case 2:printf("V"); break;
case 3:printf("IX"); break;
case 4:printf("X"); break;
case 5:printf("XL"); break;
case 6:printf("L"); break;
case 7:printf("XC"); break;
case 8:printf("C"); break;
case 9:printf("CD"); break;
case 10:printf("D"); break;
case 11:printf("CM"); break;
case 12:printf("M"); break;
}
}
}
}
printf("\n");
}
return 0;
}
求救
您的答案為: MMCCXVII
正確答案為: MCMXCVII