超时了怎么办,求大佬帮助QWQ

P1319 压缩技术

liney11 @ 2023-03-20 21:08:15


int main() {
    int n,i,j,a[100],l,m=1;
    scanf("%d", &n);
    for (i = 0; getchar() != '\n'; i++) {
        scanf("%d", &a[i]);
    }
    j = i;
    for (l=0;l<j;l++) {
        if (l % 2 == 1)
            for (i = a[l]; i > 0; i--,m++){
                printf("1");
            if (m%n == 0)
            printf("\n");
            }
        else
            for (i = a[l]; i > 0; i--,m++) {
                printf("0");                
                if (m%n == 0)
                printf("\n");
            }
    }
    return 0;
}```

by y6hz @ 2023-05-02 15:39:43

@liney11 这样写满分:

#include<iostream>
using namespace std;
int a[40000], n, i = 0, m, sum2 = 0;
int main()
{
    cin >> n;
    int sum = 0;
    while (sum < pow(n, 2))
    {
        cin >> a[i];
        sum += a[i];
        i++;
        m = i;
    }
    for (i = 0; i < m; i++)
    {
        if (i % 2 == 0)
        {
            for (int j = 0; j < a[i]; j++)
            {
                if (sum2 % n == 0 && sum2 != 0)
                {
                    cout << endl;
                }
                cout << '0';
                sum2++;
            }
        }
        else
        {
            for (int j = 0; j < a[i]; j++)
            {
                if (sum2 % n == 0 && sum2 != 0)
                {
                    cout << endl;
                }
                cout << '1';
                sum2++;
            }
        }
    }
}

by y6hz @ 2023-05-02 15:40:10

很简单,自己体会


|