求助:在C++没问题,但洛谷上0分

P1320 压缩技术(续集版)

Yang_Zhixiang @ 2023-07-16 16:16:05

下面是错误代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int N=200;
char x[N*N+5];

int reading(){
    int n=0; char c;
    cin>>x;
    n=strlen(x);
    int num=1,i=n-1;
    while(c=getchar()){
        if(i==n*n-1) break;
        if(c=='\n') c=getchar();
        x[++i] = c;
    }
    return n;
}

int main(){
    int n=reading();

    cout<<n<<' ';
    int num[3]={0}; bool flag=0;
    if(x[0]=='1'){cout<<"0 "; flag=1;}
    num[flag]++;

    for(int i=1;i<n*n;i++){
        if(x[i-1]!=x[i]){
            cout<<num[flag]<<' ';
            num[flag]=0;
            flag=!flag;
        }
        num[flag]++;
    }
    cout<<num[flag];
    return 0;
}

自测很久都没查出问题,请各位大佬救救我!


by Chlero @ 2023-08-14 14:13:37

其中一个问题是洛谷上的换行是\r而不是\n


by Yang_Zhixiang @ 2023-08-21 10:16:05

@sketchi 谢谢帮助!但是把换行改了还是不行:(


by Chlero @ 2023-08-21 11:23:38

@Yang_Zhixiang 我的代码可以给你参考一下

#include<bits/stdc++.h>
using namespace std;
int main()
{
    queue<int> a;
    bool flag=1;
    char tmp,chr='0';
    int n=0,last=0;
    while((tmp=getchar())!=EOF)
    {
        if(tmp=='\r'||tmp=='\n')
        {
            flag=0;
            continue;
        }
        if(flag)n++;
        if(tmp!=chr)
        {
            a.push(last);
            last=1,chr=tmp;
        }
        else last++;
    }
    cout<<n<<' ';
    while(a.size())
    {
        cout<<a.front()<<' ';
        a.pop();
    }
    cout<<last;
    return 0;
}

by Chlero @ 2023-08-21 11:38:05

@Yang_Zhixiang 调了一下,AC了

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int N = 200;
char x[N * N + 5];

int reading() {
    int n = 0; char c;
    cin >> x;
    n = strlen(x);
    int num = 1, i = n - 1;
    while ((c = getchar())!=EOF) {
        if (c == '\n' || c == '\r') continue;
        x[++i] = c;
    }
    return n;
}

int main() {
    int n = reading();

    cout << n << ' ';
    int num[3] = { 0 }; bool flag = 0;
    if (x[0] == '1') { cout << "0 "; flag = 1; }
    num[flag]++;

    for (int i = 1; i < n * n; i++) {
        if (x[i - 1] != x[i]) {
            cout << num[flag] << ' ';
            num[flag] = 0;
            flag = !flag;
        }
        num[flag]++;
    }
    cout << num[flag];
    return 0;
}

by Chlero @ 2023-08-21 11:38:43

@Yang_Zhixiang 提交记录


by Chlero @ 2023-08-21 12:15:15

@Yang_Zhixiang reading函数有问题


by Yang_Zhixiang @ 2023-08-22 07:18:57

@sketchi 感谢大佬!


|