为什么,解决不了输入的问题(数据最后没回车)

P1320 压缩技术(续集版)

Redeption @ 2023-10-02 11:35:59


#include <stdio.h>
int main()
{
    int n = 0, i = 0, num = 0,hr;
    char ch;
    char a[400];

    while ((ch = getchar()) != '\n')
    {
        n++;
        a[i++] = ch;
    }
    hr = n;

    for (int j = 2; j < n; j++)
    {
        for (int p = 0; p < n; p++)
        {
            ch = getchar();
            a[i++] = ch;
        }
        ch = getchar();
    }
    for (int p = 0; p < n; p++)
    {
        ch = getchar();
        a[i++] = ch;
    }

    n = i;
    ch = '0';
    printf("%d ", hr);
    for (int j = 0; j < n; j++)
    {
        if (a[j] == ch)
        {
            num++;
        }
        else
        {
            printf("%d ", num);
            num = 1;
            switch (ch)
            {
            case'1':
                ch = '0';
                break;
            case'0':
                ch = '1';
                break;
            }

        }
    }
    printf("%d", num);
    return 0;
}

by MuLinnnnn @ 2023-10-02 11:46:38

如果写C++那么可以将

while ((ch = getchar()) != '\n')
{
    n++;
    a[i++] = ch;
}

写成

while (cin >> ch)
{
    n++;
    a[i++] = ch;
}

by MuLinnnnn @ 2023-10-02 11:47:25

哦对,可以写成 while(~scanf("%c", &ch))?


by MuLinnnnn @ 2023-10-02 11:50:58

好玄学,都爆RE


by Redeption @ 2023-10-05 12:27:24

@Algophitronrhythm_ 感谢回复,现在也不太知道怎么解决:(


|