在CodeBlock上和Zerojudge的側資畫面測試都正確
但送出解答的時候顯示WA(line:7)
說我的答案為: MMMXCVII
正確答案為: MCMXCVII
#include<stdio.h>
#include<string.h>
int main() {
while(1) {
char test[10];
scanf("%s",test);
if(test[0]=='#')
break;
else {
int num1=0,num2=0,num3=0;
char rome1[11]={},rome2[10]={},rome3[20]={},output[20]={};
strcpy(rome1,test);
scanf("%s",rome2);
for(int i=0;rome1[i]!='\0'&&rome1[i]!='\n';i++) {
if(rome1[i]=='I') {
if(rome1[i+1]=='V') {
num1+=4;
i+=1;
}
else if(rome1[i+1]=='X') {
num1+=9;
i+=1;
}
else
num1+=1;
}
if(rome1[i]=='V')
num1+=5;
if(rome1[i]=='X') {
if(rome1[i+1]=='L') {
num1+=40;
i+=1;
}
else if(rome1[i+1]=='C') {
num1+=90;
i+=1;
}
else
num1+=10;
}
if(rome1[i]=='L')
num1+=50;
if(rome1[i]=='C') {
if(rome1[i+1]=='D') {
num1+=400;
i+=1;
}
else if(rome1[i+1]=='M') {
num1+=900;
i+=1;
}
else
num1+=100;
}
if(rome1[i]=='D')
num1+=500;
if(rome1[i]=='M')
num1+=1000;
}
for(int i=0;rome2[i]!='\0'&&rome2[i]!='\n';i++) {
if(rome2[i]=='I') {
if(rome2[i+1]=='V') {
num2+=4;
i+=1;
}
else if(rome2[i+1]=='X') {
num2+=9;
i+=1;
}
else
num2+=1;
}
if(rome2[i]=='V')
num2+=5;
if(rome2[i]=='X') {
if(rome2[i+1]=='L') {
num2+=40;
i+=1;
}
else if(rome2[i+1]=='C') {
num2+=90;
i+=1;
}
else
num2+=10;
}
if(rome2[i]=='L')
num2+=50;
if(rome2[i]=='C') {
if(rome2[i+1]=='D') {
num2+=400;
i+=1;
}
else if(rome2[i+1]=='M') {
num2+=900;
i+=1;
}
else
num2+=100;
}
if(rome2[i]=='D')
num2+=500;
if(rome2[i]=='M')
num2+=1000;
}
if(num1>=num2)
num3=num1-num2;
else
num3=num2-num1;
if(num3==0)
printf("ZERO\n");
else {
int j=0;
while(num3>=1000) {
rome3[j]='M';
j++;
num3-=1000;
}
while(num3>=500) {
rome3[j]='D';
j++;
num3-=500;
}
while(num3>=100) {
rome3[j]='C';
j++;
num3-=100;
}
while(num3>=50) {
rome3[j]='L';
j++;
num3-=50;
}
while(num3>=10) {
rome3[j]='X';
j++;
num3-=10;
}
while(num3>=5) {
rome3[j]='V';
j++;
num3-=5;
}
while(num3>=1) {
rome3[j]='I';
j++;
num3-=1;
}
int k=0;
for(int i=0;rome3[i]!='\0';i++) {
k++;
if(rome3[i]==rome3[i+1]&&rome3[i+1]==rome3[i+2]&&rome3[i+2]==rome3[i+3]) {
if(rome3[i]=='C') {
if(rome3[i-1]=='D') {
output[k-1]='C';
output[k]='M';
}
else {
output[k]='C';
output[k+1]='D';
}
}
if(rome3[i]=='X') {
if(rome3[i-1]=='L') {
output[k-1]='X';
output[k]='C';
}
else {
output[k]='X';
output[k+1]='L';
}
}
if(rome3[i]=='I') {
if(rome3[i-1]=='V') {
output[k-1]='I';
output[k]='X';
}
else {
output[k]='I';
output[k+1]='V';
}
}
i+=3;
}
else
output[k]=rome3[i];
}
for(int l=1;output[l]=='I'||output[l]=='V'||output[l]=='X'||output[l]=='L'||output[l]=='C'||output[l]=='D'||output[l]=='M'||output[l]=='\n';l++)
printf("%c",output[l]);
printf("\n");
}
}
}
return 0;
}