a013.
羅馬數字
--
NPSC 模擬試題
| From: [140.125.206.50] |
發表日期
:
2013-05-28 16:48
#include<stdio.h>
int numtran(char a);
int trans(int a);
char chatran(int ans);
int main()
{
int i=0,cmp1=0,cmp2=0,con=0,j=0,ansnum=0,ansnum2=0,ans =0;
char c1[100],c2[100];
scanf("%s",c1);
while (c1[0] != '#')
{
scanf("%s",c2);
cmp1=0,cmp2=0,con=0,j=0,ansnum=0,ansnum2=0,ans=0;
if (strlen(c1) == 2 && c1[0] == c1[1])
{
ansnum = numtran(c1[0]) + numtran(c1[1]);
goto c1e;
}
for (i=strlen(c1)-1;i!=0;i--)
{
if (numtran(c1[i]) == numtran(c1[i-1])) ansnum = ansnum + numtran(c1[i]);
if (numtran(c1[i]) < numtran(c1[i-1])) ansnum = ansnum + numtran(c1[i]);
if (numtran(c1[i]) > numtran(c1[i-1]))
{
ansnum = ansnum + numtran(c1[i]) - numtran(c1[i-1]);
if (i != 1) i--;
}
}
c1e:
if (strlen(c1) > 2 || strlen(c1) == 1) ansnum = ansnum + numtran(c1[0]);
if (strlen(c2) == 2 && c2[0] == c2[1])
{
ansnum2 = numtran(c2[0]) + numtran(c2[1]);
goto c2e;
}
for (i=strlen(c2)-1;i!=0;i--)
{
if (numtran(c2[i]) == numtran(c2[i-1])) ansnum2 = ansnum2 + numtran(c2[i]);
if (numtran(c2[i]) < numtran(c2[i-1])) ansnum2 = ansnum2 + numtran(c2[i]);
if (numtran(c2[i]) > numtran(c2[i-1]))
{
ansnum2 = ansnum2 + numtran(c2[i]) - numtran(c2[i-1]);
if (i != 1) i--;
}
}
c2e:
if (strlen(c2) > 2|| strlen(c2) == 1) ansnum2 = ansnum2 + numtran(c2[0]);
ans = abs(ansnum - ansnum2);
if (ans == 0) printf("ZERO");
if (ans != 0)
{
chatran(ans);
}
printf("\n");
scanf("%s",c1);
}
return 0;
}
int numtran(char a)
{
if (a == 'I') return 1;
if (a == 'V') return 5;
if (a == 'X') return 10;
if (a == 'L') return 50;
if (a == 'C') return 100;
if (a == 'D') return 500;
return 1000;
}
int trans(int a)
{
if (a == 1) printf("I");
if (a == 5) printf("V");
if (a == 10) printf("X");
if (a == 50) printf("L");
if (a == 100) printf("C");
if (a == 500) printf("D");
if (a == 1000) printf("M");
if (a == 4) printf("IV");
if (a == 9) printf("IX");
if (a == 40) printf("XL");
if (a == 90) printf("XC");
if (a == 400) printf("CD");
if (a == 900) printf("CM");
}
char chatran(int ans)
{
int ansend[10],i=0,j=0,k=0;
j=10,i=0;
do
{
ansend[i]=ans%j;
ans-=ans%j;
j*=10,i+=1;
}
while(ans != 0);
j/=100;
i--;
do
{
if (ansend[i] / j == 9 || ansend[i] / j == 4)
{
trans(ansend[i]);
}
else
{
if (ansend[i] / j != 5)
{
if (ansend[i] / j > 5)
{
trans(5*j);
ansend[i] = ansend[i] - 5 * j;
}
do
{
trans(1 * j);
ansend[i] -=1*j;
}
while (ansend[i] != 0);
}
if (ansend[i] / j == 5) trans(ansend[i]);
}
j/=10;
i--;
}
while(i > -1);
}
int tem(int a)
{
while (a > 10)
{
a = a / 10;
}
return a;
}
TLE(1s)逾時
請問要如何讓它執行速度變快? 或者它是其他錯誤?