我这个程序为什么测试的时候正确,但全WA

P1320 压缩技术(续集版)

Gordon1 @ 2024-04-21 16:21:20

#include<iostream>
#include<cmath>
using namespace std;
int a[40010];
char b[40010];
int main(void)
{
    int i=1;
    while(cin>>b[i])
    {
        if(b[i]!='0'&&b[i]!='1')
        {
            break;
        }
        a[i]=b[i]-'0';
        ++i;
    }
    cout<<sqrt(i-1)<<" ";
    a[i]=2;
    bool c=false;
    int count=0;
    for (int j=1;j<=i; )
    {
        if(a[j]==c)
        {
            count+=1;
            ++j;
        }
        else if(a[j]==!c)
        {
            cout<<count<<" ";
            count=0;
            c=!c;
        }
        else
        {
            cout<<count<<" ";
            return 0; 
        }
    }
}

请大家给我讲解一下


by colin526 @ 2024-05-04 16:50:22

11行可能have点问题
if(b[i] != '0' && b[i] != 1)
例如例题,当输入完0001000后”换行“也属于!=’0‘ &&!=’1‘,导致只输入n个数后便停止
可以把整个换成
if(b[i] == '0' || b[i] == '1')n++;统计n

后面不太清楚,因为我bush这么做的其实是看不懂自己检查一下


by guoyize @ 2024-05-26 09:37:19


#include <bits/stdc++.h>
using namespace std;
int main()
{
string p;
cin>>p;
int c=p.size(),a[c*c],i,s,j;
bool f=0;
for(j=0;j<c;j++)
    a[j]=p[j]-'0';
for(i=1;i<c;i++)
{
    cin>>p; 
    for(j=i*c;j<i*c+c;j++)
        a[j]=p[j-i*c]-'0';
}
cout<<c<<" ";
i=0;
while(i<c*c)
{
    s=0;
    while(a[i]==f&&i<c*c)
    {
        s++;
        i++;
    }
    f=!f;
    cout<<s<<" ";
}
return 0;
}
有一个测试点是
0000000
0000000
0000000
0000000
0000000
0000000
我做的时候也卡了70

@Gordon1


by guoyize @ 2024-05-26 09:39:54


~~额~~
这样就AC

了

by wangqicheng1637 @ 2024-06-12 20:14:16

@guoyize 测试点太猥琐


by guoyize @ 2024-06-16 10:57:45

@wangqicheng1637确实


|