a013.
羅馬數字
--
NPSC 模擬試題
| From: [1.162.135.116] |
發表日期
:
2013-04-15 13:45
WA (line:2)
答案不正確
您的答案為: ZERO
正確答案為: MCMXCVIII
我使用visual c++測試可以通過
可是為什麼丟上來之後卻會出現上面的錯誤?
有人可以告訴我問題出在哪嗎?
以下是我的程式碼
#include <stdio.h>
#include <string.h>
int decode(char *code){
int value = 0, i, iplus = 0, xplus = 0, cplus = 0;
for(i = strlen(code) - 1; i >= 0; i--){
switch(code[i]){
case 'I':
if(iplus == 0)
value += 1;
else
value -= 1;
break;
case 'V':
iplus++;
value += 5;
break;
case 'X':
iplus++;
if(xplus == 0)
value += 10;
else
value -= 10;
break;
case 'L':
xplus++;
value += 50;
break;
case 'C':
xplus++;
if(cplus == 0)
value += 100;
else
value -= 100;
break;
case 'D':
cplus++;
value += 500;
break;
case 'M':
cplus++;
value += 1000;
break;
default:
printf("Input value fail");
break;
}
}
return value;
}
char *encode(int value){
char code[16];
int i;
for(i = 0; i < 15 && value > 0; i++){
while(value >= 1000){
code[i++] = 'M';
value -= 1000;
}
while(value > 900){
code[i++] = 'C';
code[i++] = 'M';
value -= 900;
}
while(value >= 500){
code[i++] = 'D';
value -= 500;
}
while(value >= 100){
code[i++] = 'C';
value -= 100;
}
while(value > 90){
code[i++] = 'X';
code[i++] = 'C';
value -= 90;
}
while(value >= 50){
code[i++] = 'L';
value -= 50;
}
while(value >= 10){
code[i++] = 'X';
value -= 10;
}
while(value > 9){
code[i++] = 'I';
code[i++] = 'X';
value -= 9;
}
while(value >= 5){
code[i++] = 'V';
value -= 5;
}
while(value >= 1){
code[i++] = 'I';
value -= 1;
}
}
code[i - 1] = '\0';
return code;
}
int main(){
char buff[16], a[16];
char *pch;
int x[2];
while(scanf("%s", buff)){
if(buff[0] == '#')
break;
x[0] = decode(buff);
scanf("%s", buff);
x[1] = decode(buff);
if(x[0] == x[1])
strcpy(a, "ZERO");
else if(x[0] > x[1])
strcpy(a, encode(x[0] - x[1]));
else if(x[0] < x[1])
strcpy(a, encode(x[1] - x[0]));
printf("%s\n", a);
}
return 0;
}