a013.
羅馬數字
--
NPSC 模擬試題
| From: [140.113.124.40] |
發表日期
:
2012-08-21 12:24
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <math.h>
int main(void){
char a[1000];
char b[1000];
int c[1000],d[1000];
c[0]=INT_MAX;
d[0]=INT_MAX;
int counta=0;
int countb=0;
int result;
while(scanf("%s",&a)!=EOF&&a[0]!='#'){
scanf("%s",&b);
int i,j;
for(i=0;i<strlen(a);i++){
switch(a[i]){
case 'I':
c[i+1]=1;
break;
case 'V':
c[i+1]=5;
break;
case 'X':
c[i+1]=10;
break;
case 'L':
c[i+1]=50;
break;
case 'C':
c[i+1]=100;
break;
case 'D':
c[i+1]=500;
break;
case 'M':
c[i+1]=1000;
break;
}
}
for(j=0;j<strlen(b);j++){
switch(b[j]){
case 'I':
d[j+1]=1;
break;
case 'V':
d[j+1]=5;
break;
case 'X':
d[j+1]=10;
break;
case 'L':
d[j+1]=50;
break;
case 'C':
d[j+1]=100;
break;
case 'D':
d[j+1]=500;
break;
case 'M':
d[j+1]=1000;
break;
}
}
for(i=0;i<strlen(a);i++){
if(c[i]>=c[i+1]){
counta=counta+c[i+1];
}else{
counta=counta+c[i+1]-2*c[i];
}
}
for(j=0;j<strlen(b);j++){
if(d[j]>=d[j+1]){
countb=countb+d[j+1];
}else{
countb=countb+d[j+1]-2*c[j];
}
}
result=(int)fabs(counta-countb);
if(result==0){
puts("ZERO");
}else{
while(result>=1000){
printf("M");
result-=1000;
}
if(result>=900){
printf("CM");
result-=900;
}
if(result>=500){
printf("D");
result-=500;
}
if(result>=400){
printf("CD");
result-=400;
}
while(result>=100){
printf("C");
result-=100;
}
if(result>=90){
printf("XC");
result-=90;
}
if(result>=50){
printf("L");
result-=50;
}
if(result>=40){
printf("XL");
result-=40;
}
while(result>=10){
printf("X");
result-=10;
}
if(result>=9){
printf("IX");
result-=9;
}
if(result>=5){
printf("V");
result-=5;
}
if(result>=4){
printf("IV");
result-=4;
}
while(result>=1){
printf("I");
result-=1;
}
result=0;
counta=0;
countb=0;
printf("\n");
}
for(i=0;i<strlen(a);i++){
a[i]='\0';
}
for(j=0;j<strlen(b);j++){
b[i]='\0';
}
for(i=0;i<strlen(a);i++){
c[i+1]=0;
}
for(j=0;j<strlen(b);j++){
d[i+1]=0;
}
}
return 0;
}
反覆檢查了好多次都不知道問題在哪
煩請高手幫忙