看了好久,一步步检查,还是不知道哪里错了,求助

P1319 压缩技术

thliup @ 2023-12-13 14:20:35

#include<iostream>
using namespace std;
#include<iomanip>
#include<cmath>
#include<algorithm>
int main(){ int a[1000000] = { 0 };
int n;
 cin >> n;
int t, i, p = 0, ans = 0, k = 0;
while (cin >> t) {
    for (i = 1 + p; i <= t + p; i++) {
        a[i] = k;
    }
    p = i;
    k = 1 - k;
}
for (i = 1; i <= n * n; i++) {
    ans++;
    cout << a[i];
    if (ans == n) {
        cout << endl;
        ans = 0;
    }
}

return 0;
}

by _IceCream_ @ 2023-12-13 14:37:29

#include<iostream>
#include<iomanip>
#include<cmath>
#include<algorithm>
using namespace std;
int t, i, p = 0, ans = 0, k = 0;
int a[1000000] = { 0 };
int n;
int main(){ 
    cin >> n;
    while (cin >> t) {
        for (i = 1 + p; i <= t + p; i++) {
            a[i] = k;
        }
        p += t;
        k = 1 - k;
    }
    for (i = 1; i <= n * n; i++) {
        ans++;
        cout << a[i];
        if (ans == n) {
            cout << endl;
            ans = 0;
        }
    }

    return 0;
}

变量 p 不应该直接修改为 i ,而应该是 += i


by _IceCream_ @ 2023-12-13 14:37:57

@Vlixel 口误,+=t


by thliup @ 2023-12-14 19:23:28

@Vlixel 为什么啊,等于i为什么错呢?谢谢


|