70分,但wa,洛谷的在线ide也过了

P1320 压缩技术(续集版)

Sigma10086 @ 2023-10-06 17:13:05

#include <bits/stdc++.h>
using namespace std;
char c;
int tot, n, num, n_num, tot1;
bool first, firch, is_zero;
vector<int> ans;
int main()
{
    while (cin.get(c))
    {
        if (!firch)
        {
            if (c == '0')
            {
                is_zero = true;
            }
            else
            {
                is_zero = false;
                ans.push_back(0);
            }
            firch = true;
        }
        if (c == '0')
        {
            if (is_zero)
            {
                num++;
            }
            else
            {
                ans.push_back(num);
                num = 1;
                is_zero = true;
            }
        }
        else if (c == '1')
        {
            if (!is_zero)
            {
                num++;
            }
            else
            {
                ans.push_back(num);
                num = 1;
                is_zero = false;
            }
        }
        if (!first)
        {
            tot++;
            if (c == '\n' || c == '\r')
            {
                n = tot - 1;
                first = true;
                n_num = 1;
                continue;
            }
        }
        if (first)
        {
            if (n_num == n - 1)
            {
                tot1++;
            }
            if (c == '\n' || c == '\r')
            {
                n_num++;
            }
            if (tot1 == n)
            {
                break;
            }
        }
    }
    ans.push_back(num);
    cout << n << " ";
    for (int i = 0; i < (int)ans.size(); i++)
    {
        cout << ans[i] << " ";
    }
    return 0;
}

测试点1一直wa,本地自测与洛谷在线ide测试与答案一样,想不明白,换行符也同时判断了\n与\r了(没改之前全wa)。翻了下之前的提问帖说不能用单个字符输入,是这么做不妥当吗

测试点1输入

11111
00100
11111
00100
11111

测试点1输出

5 0 5 2 1 2 5 2 1 2 5 

by XNULL666 @ 2023-10-06 21:44:42

建议使用:

while(1){
    scanf(" %c",&c);
    ....
}

" %c"的空格不能删


|