谁能帮忙看看我写的错哪了

P1319 压缩技术

wy18294650245 @ 2023-06-27 16:38:02

#include<iostream>
using namespace std;

int main()
{
    int N;
    cin >> N;

    int a,b;
    int s = 0;
    while(s != N*N)
    {
        cin >> a >> b;
        int i = 0;
        for(i = 0;i < a;i++)
        {
            cout << 0;
            s++;
            if(s%N==0)
            {
                cout << endl;
            }
        }
        for(i = 0;i < b;i++)
        {
            cout << 1;
            s++;
            if(s%N==0)
            {
                cout << endl;
            }
        }
    }
    return 0;
}

by CSP_zyh @ 2023-07-17 20:24:44

我帮你改了一下,用两个循环会超时,我加了一个flag,用一次改一次,变成一个循环

#include<iostream>
using namespace std;
bool flag;
int main()
{
    int N;
    cin >> N;
    int a;
    int s = 0;
    flag=1;
    while(s != N*N)
    {
        cin >> a;
        int i = 0;
        flag=1-flag;//重点是这个!
        for(i = 0;i < a;i++)
        {
            if(!flag){
                cout << 0;
            }
            else{
                cout << 1;
            }
            s++;
            if(s%N==0)
            {
                cout << endl;
            }
        }
    }
    return 0;
}

by CSP_zyh @ 2023-07-17 20:28:51

另外,可以给个关注吗qwq


by douyachong @ 2023-09-04 20:25:40

@CSP_zyh 给了


|