刚刚学C语言,在某个Online Judge上的一道题:
—————————————————————
input:输入一行字符。
output:字母数,空格数,数字数,其它字符数,结果用一行表示,数字中间用空格隔开。
—————————————————————
题目很简单。但提交后总是Time Limit Exceed.
我的代码:
# include <stdio.h>
int main ()
{
int x,i,y,j;
x=i=y=j=0;
char a;
while (scanf("%c",&a)&&a!='\n')
{
if(('a'<=a&&a<='z')||('A'<=a&&a<='Z'))
x++;
else if(a==' ') y++;
else if('0'<=a&&a<='9') i++;
else j++;
}
printf("%d %d %d %d\n",x,y,i,j);
return 0;
}
请大神帮忙改进~~~
—————————————————————
input:输入一行字符。
output:字母数,空格数,数字数,其它字符数,结果用一行表示,数字中间用空格隔开。
—————————————————————
题目很简单。但提交后总是Time Limit Exceed.
我的代码:
# include <stdio.h>
int main ()
{
int x,i,y,j;
x=i=y=j=0;
char a;
while (scanf("%c",&a)&&a!='\n')
{
if(('a'<=a&&a<='z')||('A'<=a&&a<='Z'))
x++;
else if(a==' ') y++;
else if('0'<=a&&a<='9') i++;
else j++;
}
printf("%d %d %d %d\n",x,y,i,j);
return 0;
}
请大神帮忙改进~~~
