#13250: [C] 測試執行RE求助


CSE310732 (KenZheng)

學校 : 國立臺中高級工業職業學校
編號 : 47091
來源 : [125.224.242.123]
最後登入時間 :
2018-02-14 21:53:26
a013. 羅馬數字 -- NPSC 模擬試題 | From: [114.46.134.35] | 發表日期 : 2018-01-15 17:21

在自己電腦用VS 和 Dev這兩個IDE測測資都正常,但是丟上測試執行會出現

RE (SIGSEGV)

記憶體區段錯誤! Segmentation fault (core dumped)



Code:

#include <stdio.h>
#include <string.h>

char roman[7] = { 'I','V','X','L','C','D','M' };
char *toroman[13] = { "M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I" };
int number[7] = { 1,5,10,50,100,500,1000 };
int tonumber[13] = { 1000,900,500,400,100,90,50,40,10,9,5,4,1 };

int InputRoman(char *pch)
{
int value = 0, c1 = 1001, c2;
for (int i = 0; i < (int)strlen(pch); i++)
{
for (int j = 0; j < 7; j++)
{
if (pch[i] == roman[j])
{
c2 = number[j];
value += number[j];
break;
}
}
if (c2 > c1)
value -= c1 * 2;
c1 = c2;
}
return value;
}

void OutputRoman(int ans)
{
while (ans)
{
for (int i = 0; i < 13; i++)
{
if (ans >= tonumber[i])
{
ans -= tonumber[i];
printf("%s", toroman[i]);
}
}
}
printf("\n");
}

int main()
{
char input[16];
while (strcmp(gets(input),"#") != 0)
{
int num[2],cnt = 0;
char *pch;
pch = strtok(input, " ");
while (pch != NULL)
{
num[cnt] = InputRoman(pch);
cnt++;
pch = strtok(NULL, " ");
}
int ans = num[0] - num[1];
if (ans)
OutputRoman(ans);
else
printf("ZERO\n");
}
return 0;
}

 
ZeroJudge Forum