a013.
羅馬數字
--
NPSC 模擬試題
| From: [114.25.71.85] |
發表日期
:
2013-01-23 16:06
#include <stdio.h>
#include <math.h>
int loma(char c1[])
{
int i,n1=0;
for(i=0;c1[i]!='\0';i++){
if(c1[i]=='C'){
if(c1[i+1]=='M')
{ n1+=900; i++; continue;}
if(c1[i+1]=='D')
{ n1+=400; i++; continue;} }
if(c1[i]=='X'){
if(c1[i+1]=='C')
{ n1+=90; i++; continue;}
if(c1[i+1]=='L')
{ n1+=40; i++; continue;} }
if(c1[i]=='I'){
if(c1[i+1]=='X')
{ n1+=9; i++; continue;}
if(c1[i+1]=='V')
{ n1+=4; i++; continue;} }
switch(c1[i]){
case 'M': n1+=1000; break;
case 'D': n1+=500; break;
case 'C': n1+=100; break;
case 'L': n1+=50; break;
case 'X': n1+=10; break;
case 'V': n1+=5; break;
case 'I': n1+=1; break;
}
}
return n1;
}
void num(int n)
{
while(n>999){ printf("M"); n-=1000;}
if(n/100==9){ printf("CM"); n-=900;}
if(n/100==4){ printf("CD"); n-=400;}
while(n>499){ printf("D"); n-=500;}
while(n>99) { printf("C"); n-=100;}
if(n/10==9) { printf("XC"); n-=90;}
if(n/10==4) { printf("XL"); n-=40;}
while(n>49) { printf("L"); n-=50;}
while(n>9) { printf("X"); n-=10;}
if(n==9) { printf("IX"); n-=9;}
if(n==4) { printf("IV"); n-=4;}
while(n>4) { printf("V"); n-=5;}
while(n>0) { printf("I"); n-=1;}
printf("\n");
}
int main()
{
int n1,n2,max,min;
char c1[10],c2[10];
while(scanf("%s",c1)){
if(c1[0]=='#') break;
scanf("%s",c2);
n1=loma(c1);
n2=loma(c2);
n1=abs(n1-n2);
if(n1==0) printf("ZERO\n");
else num(n1);}
return 0;
}
WA (line:18)答案不正確您的答案為: MMCDLXVII ...略
正確答案為: MCXI
我有用絕對值了,也有測過2000-889,都正確,請各位告訴我錯誤在哪?