计算古罗马加法,输入不合法,则输出“Aha! I don't need to calculate the sum”。
| |||||
测试用例 1 |
|
| 1秒 | 64M | 0 |
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char p[5][11][10];
void dabiao()
{
memset(p,0,sizeof(p));
strcpy(p[1][1],"I");
strcpy(p[1][2],"II");
strcpy(p[1][3],"III");
strcpy(p[1][4],"IV");
strcpy(p[1][5],"V");
strcpy(p[1][6],"VI");
strcpy(p[1][7],"VII");
strcpy(p[1][8],"VIII");
strcpy(p[1][9],"IX");
strcpy(p[2][1],"X");
strcpy(p[2][2],"XX");
strcpy(p[2][3],"XXX");
strcpy(p[2][4],"XL");
strcpy(p[2][5],"L");
strcpy(p[2][6],"LX");
strcpy(p[2][7],"LXX");
strcpy(p[2][8],"LXXX");
strcpy(p[2][9],"XC");
strcpy(p[3][1],"C");
strcpy(p[3][2],"CC");
strcpy(p[3][3],"CCC");
strcpy(p[3][4],"CD");
strcpy(p[3][5],"D");
strcpy(p[3][6],"DC");
strcpy(p[3][7],"DCC");
strcpy(p[3][8],"DCCC");
strcpy(p[3][9],"CM");
strcpy(p[4][1],"M");
strcpy(p[4][2],"MM");
}
char a[20],b[20];
int aa[20],bb[20];
int main()
{
dabiao();
scanf("%s",a);
scanf("%s",b);
int la=strlen(a);
int lb=strlen(b);
int za=0,zb=0;
int i,j,k;
int I=1,V=5,X=10,L=50,C=100,D=500,M=1000;
memset(aa,0,sizeof(aa));
memset(bb,0,sizeof(bb));
for(i=0;i<la;i++)
{
if(a[i]=='I')
aa[i]=I;
else if(a[i]=='V')
aa[i]=V;
else if(a[i]=='X')
aa[i]=X;
else if(a[i]=='L')
aa[i]=L;
else if(a[i]=='C')
aa[i]=C;
else if(a[i]=='D')
aa[i]=D;
else if(a[i]=='M')
aa[i]=M;
}
for(i=0;i<lb;i++)
{
if(b[i]=='I')
bb[i]=I;
else if(b[i]=='V')
bb[i]=V;
else if(b[i]=='X')
bb[i]=X;
else if(b[i]=='L')
bb[i]=L;
else if(b[i]=='C')
bb[i]=C;
else if(b[i]=='D')
bb[i]=D;
else if(b[i]=='M')
bb[i]=M;
}
for(i=0;i<la;i++)
{
if(aa[i]>=aa[i+1])
za+=aa[i];
else{
za+=aa[i+1]-aa[i];
i++;
}
}
for(i=0;i<lb;i++)
{
if(bb[i]>=bb[i+1])
zb+=bb[i];
else{
zb+=bb[i+1]-bb[i];
i++;
}
}
int flag=0;
char ta[20];
char tb[20];
memset(ta,0,sizeof(ta));
memset(tb,0,sizeof(tb));
int temp=za/1000;
if(temp!=0)
strcat(ta,p[4][temp]);
temp=(za/100)%10;
if(temp!=0)
strcat(ta,p[3][temp]);
temp=(za/10)%10;
if(temp!=0)
strcat(ta,p[2][temp]);
temp=za%10;
if(temp!=0)
strcat(ta,p[1][temp]);
if(strcmp(a,ta)!=0)
flag=1;
temp=zb/1000;
if(temp!=0)
strcat(tb,p[4][temp]);
temp=(zb/100)%10;
if(temp!=0)
strcat(tb,p[3][temp]);
temp=(zb/10)%10;
if(temp!=0)
strcat(tb,p[2][temp]);
temp=zb%10;
if(temp!=0)
strcat(tb,p[1][temp]);
if(strcmp(b,tb)!=0)
flag=1;
if(flag==0){
int sum=za+zb;
int temp=sum/1000;
if(temp!=0)
printf("%s",p[4][temp]);
temp=(sum/100)%10;
if(temp!=0)
printf("%s",p[3][temp]);
temp=(sum/10)%10;
if(temp!=0)
printf("%s",p[2][temp]);
temp=sum%10;
if(temp!=0)
printf("%s",p[1][temp]);
printf("\n");
}
else
printf("Aha! I don't need to calculate the sum\n");
system("pause");
}