0分?样例和下载的测试数据都正确啊?什么鬼

P1320 压缩技术(续集版)

cmd137 @ 2023-10-30 20:04:18

而且WA:提供的信息是把我输出的数n读为n+1了??? 以下是源码:```cpp

include <iostream>

using namespace std; int a[20005];

int main() { int n = 0; char t = '0'; char last = '0'; int p = 1; int N = 0; while (1) {

    t = getchar();

    if (t == '\n') {
        n = N;
        break;
    }

    if (t == last) {
        a[p]++;
    } else {
        a[++p]++;
        last = t;
    }
    N++;
}
for (int i = 1; i <= (n - 1) * (n + 1); i++) {
    t = getchar();

    if (t == '\n') {
        continue;
    }

    if (t == last) {
        a[p]++;
    } else {
        a[++p]++;
        last = t;
    }
}

cout << n << " ";
for (int i = 1; i <= p; i++) {
    cout << a[i] << " ";
}

}


by _zhang @ 2023-10-30 20:21:47

《关于lz的md炸了的这件事》


by _zhang @ 2023-10-30 20:45:07

@cmd137 还有lz,你t = getchar()好像输入了两遍诶,


by _zhang @ 2023-10-30 20:46:12

先把t存下来再操作


by cmd137 @ 2023-10-31 19:11:52

@_zhang 第一次是为了获取每行几个,即n,后面处理其他数据,所以到底为什么我答案为什么正确判断的时候却被+1了


by _zhang @ 2023-10-31 19:22:33

@cmd137 其实你完全可以把求N和求压缩码放一块的


by _zhang @ 2023-10-31 19:39:10

@cmd137 找到你的错误了,就是这里:

for (int i = 1; i <= (n - 1) * (n + 1); i++) {

你的*(n+1)是片面的,有可能行末有空格或空行\ 因此,你要判断的是t是否为'0'或'1',而不是'\n' 我帮你改了下代码:

#include <iostream>
#include <cstdio>
using namespace std;
int a[20005];
int main() {
    int n = 0;
    char t = '0';
    char last = '0';
    int p = 1;
    int N = 0;
    while (1) {
        t = getchar();
        if (t != '1' && t != '0') {
            n = N;
            break;
        }
        if (t == last) {
            a[p]++;
        } else {
            a[++p]++;
            last = t;
        }
        N++;
    }
    for (int i = 1; i <= (n - 1) * n; ) {
        t = getchar();
        if (t != '1' && t != '0') continue;
        if (t == last) {
            a[p]++;
        } else {
            a[++p]++;
            last = t;
        }
        i++;
    }
    cout << n << " ";
    for (int i = 1; i <= p; i++) cout << a[i] << " ";
    return 0;
}

切记不要抄,棕名两行泪


by _zhang @ 2023-10-31 19:43:36

这也就是代码中用getchar()的弊端,另外无耻的求个关注


by cmd137 @ 2023-10-31 20:29:39

@_zhang 已关注大佬,还是有点不懂为什么我答案明明能对上却WA了,还有如果如你所说,我试着将条件改为(t == '\n' || t == ' ' || t == '\0')还是不过,求解答,


by cmd137 @ 2023-10-31 20:35:58

@_zhang 另外,对于(n+1) 变成n,如果getchar()接收到空格之类的,不也是算一次循环吗?continue之后跳过本次循环不就会接受不到后边n-1个字符了吗,有点懵


by NumberTrart @ 2023-11-01 19:46:32

行末的符号是ASCII 13

别问我怎么知道的


| 下一页