注册 登录
编程论坛 C语言论坛

不知道这个代码有啥问题,为啥运行时间超限

Y07 发布于 2021-07-10 02:04, 1798 次点击
#include<stdio.h>
int main()
{
    int chara = 0, num = 0, space = 0, other = 0;
    int ch;
    ch = getchar();
    while (ch != '\n')
    {
        if (ch>= 'A' && ch<= 'Z'||ch>='a'&&ch<='z')
        {
            chara++;
        }
        else if (ch>= '0' && ch<= '9')
        {
            num++;
        }
        else if (ch == ' ')
        {
            space++;
        }
        else
            other++;
        
    }
    printf("%d %d %d %d\n", chara, num, space, other);

    return 0;
}
4 回复
#2
rjsp2021-07-10 11:21

ch = getchar();
while (ch != '\n')
改为
while (ch=getchar(), ch!='\n')
试试
#3
邹德华2021-07-10 12:58
我觉得你这逻辑有一点错误:
    这个题目应该是统计一串输入字符各个的个数。
    但是ch却是一个字符,应该使用字符数组。然后应该就做得出了。
#4
zbjzbj2021-07-10 19:27
回复 3楼 邹德华
不对。照2楼的做。
#5
王牌疯姐姐2021-07-20 19:46
#include<stdio.h>
int main()
{
    int chara=0,num=0,space=0,other=0;
    int ch;
    ch=getchar();
   
      if (ch>='A'&&ch<='Z'||ch>='a'&&ch<='z')
        {
            chara++;
        }
        else if (ch>='0'&&ch<='9')
        {
            num++;
        }
        else if (ch=='0')
        {
            space++;
        }
        else
            other++;
        
   
    printf("%d %d %d %d\n", chara,num,space,other);

    return 0;
}
我不知道你这个程序什么意思,然后就这么改了
1