大佬们帮忙看看,cpp

P1319 压缩技术

alexxs60 @ 2023-11-25 18:30:26

#include<iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    int flag = 0;
    int count = 0;
    while (1)
    {

        int x;

        cin >> x;
        for (int j = 0; j < x; j++)
        {
            if (flag == 0)
            {

                if (count % n == 0)
                {
                    cout << endl;
                }
                cout << 0;
                count++;
                if (count == n * n)
                {
                    return 0;
                }

            }
            else if (flag == 1)
            {

                if (count % n == 0)
                {
                    cout << endl;
                }
                cout << 1;
                count++;
                if (count == n * n)
                {
                    return 0;
                }

            }
        }
        if (flag == 0)
        {
            flag = 1;
        }
        else if (flag == 1)
        {
            flag = 0;
        }
    }

    return 0;
}

by _IceCream_ @ 2023-12-13 14:44:56

#include<iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    int flag = 0;
    int count = 0;
    while (1)
    {

        int x;

        cin >> x;
        for (int j = 0; j < x; j++)
        {
            if (flag == 0)
            {

                cout << 0;
                count++;
                if (count == n * n)
                {
                    return 0;
                }
                if (count % n == 0)
                {
                    cout << endl;
                }

            }
            else if (flag == 1)
            {

                cout << 1;
                count++;
                if (count == n * n)
                {
                    return 0;
                }
                if (count % n == 0)
                {
                    cout << endl;
                }

            }
        }
        if (flag == 0)
        {
            flag = 1;
        }
        else if (flag == 1)
        {
            flag = 0;
        }
    }

    return 0;
}

@alexxs60 调换一下顺序,你的程序没有错,顺序错了,先自增换行。


by _IceCream_ @ 2023-12-13 14:45:28

@Vlixel 先自增后换行


|