如題,我是用codeblock寫完再複製到網站上,但複製到這裡卻跳出TLE,我看了很久但完全不知道問題出在哪,程式碼如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Roman_to_num(void);
void num_to_Roman(void);
char RN[100];
int i=0, ans=0, j;
void Roman_to_num()
{
while(RN[0]!='#'&&RN[i]!='\0')
{
switch(RN[i])
{
case 'I':
if(RN[i+1]=='V'||RN[i+1]=='X')
{
continue;
}
else
{
ans=ans+1;
}
break;
break;
case 'V':
if(RN[i-1]=='I')
{
ans=ans+4;
}
else
{
ans=ans+5;
}
break;
case 'X':
if(RN[i-1]=='I')
{
ans=ans+9;
}
else if(RN[i+1]=='L'||RN[i+1]=='C')
{
continue;
}
else
{
ans=ans+10;
}
break;
case 'L':
if(RN[i-1]=='X')
{
ans=ans+40;
}
else
{
ans=ans+50;
}
break;
case 'C':
if(RN[i-1]=='X')
{
ans=ans+90;
}
else if(RN[i+1]=='D'||RN[i+1]=='M')
{
continue;
}
else
{
ans=ans+100;
}
break;
case 'D':
if(RN[i-1]=='C')
{
ans=ans+400;
}
else
{
ans=ans+500;
}
break;
case 'M':
if(RN[i-1]=='C')
{
ans=ans+900;
}
else
{
ans=ans+1000;
}
break;
case ' ':
ans=-ans;
break;
}
i++;
}
if(ans<0)
{
ans=-ans;
}
else if(ans==0)
{
printf("ZERO");
}
}
void num_to_Roman()
{
// k=1000
j=ans/1000;
while(j>0)
{
printf("M");
j--;
}
ans=ans%1000;
// k=100
j=ans/100;
if(j==9)
{
printf("CM");
}
else if(j>5&&j<9)
{
printf("D");
j=j-5;
while(j>0)
{
printf("C");
j--;
}
}
else if(j==4)
{
printf("CD");
}
else if(j<4&&j>0)
{
while(j>0)
{
printf("C");
j--;
}
}
ans=ans%100;
// k=10
j=ans/10;
if(j==9)
{
printf("XC");
}
else if(j>5&&j<9)
{
printf("L");
j=j-5;
while(j>0)
{
printf("X");
j--;
}
}
else if(j==4)
{
printf("XL");
}
else if(j<4&&j>0)
{
while(j>0)
{
printf("X");
j--;
}
}
ans=ans%10;
// 1
j=ans;
if(j==9)
{
printf("IX");
}
else if(j>5&&j<9)
{
printf("V");
j=j-5;
while(j>0)
{
printf("I");
j--;
}
}
else if(j==4)
{
printf("IV");
}
else if(j<4&&j>0)
{
while(j>0)
{
printf("I");
j--;
}
}
}
int main()
{
gets(RN);
while(RN[0]!='#')
{
Roman_to_num();
if(ans!=0)
{
num_to_Roman();
}
memset(RN,0,sizeof(RN));
gets(RN);
}
if(RN[0]!='#')
{
exit(0);
}
return 0;
}