c++蒟蒻求助

P1319 压缩技术

xyy_xxyy @ 2024-01-19 16:26:48

0分,全WA。。。

代码:

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    int x = 1;
    int a;
    int sum = 0;
    int f = 0;
    int cik = 0;
    while (x <= n * n) {
        cin >> a;
        for (int i = 1; i <= a; i++) {
            cout << f;
            cik++;
            if (cik % n  == 0) {
                cout << "\n";
            }
        }
        f = !f;
        x++;
    }
    return 0;
}

by TryHardToBeAlive @ 2024-01-19 16:32:01

@xyy_xxyy 你的while循环退出条件不应是x<=n*n,而应该是cik<=n*n,因为n*n求的是输出的汉字点阵图字符总数,要是像你这么写就变成了输入的压缩码字符总数小于等于n,这是不对的。


by xyy_xxyy @ 2024-01-19 16:34:37

@youzhanyue 谢谢大佬%%%,已关。


by TryHardToBeAlive @ 2024-01-19 16:35:35

@xyy_xxyy 停,没A

不好意思我还没改完


by TryHardToBeAlive @ 2024-01-19 16:42:54

@xyy_xxyy A了

第一个错误改正后,变成了cik<=n*n,但还不能AC,因为在代码输出完了最后一个字符后,cik加到了n*n,而这一次while循环结束后判断条件时,cik还是等于n*n,所以还会多吃一次输入,这造成输入完了代码还要求要输入一个数。至于改正,只需要将小于等于改成小于就好了。

代码:


#include<bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    int a;
    int f = 0;
    int cik = 0;
    while (cik < n * n) {
        cin >> a;
        for (int i = 1; i <= a; i++) {
            cout << f;
            cik++;
            if (cik % n == 0) {
                cout << "\n";
            }
        }
        f = !f;
    }
    return 0;
}
```cpp

by xyy_xxyy @ 2024-01-19 16:43:20

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    int a;
    int sum = 0;
    int f = 0;
    int cik = 1;
    while (cik <= n * n) {
        cin >> a;
        for (int i = 1; i <= a; i++) {
            cout << f;
            cik++;
            if (cik % n  == 1) {
                cout << "\n";
            }
        }
        f = !f;
    }
    return 0;
}

%的时候错了


by TryHardToBeAlive @ 2024-01-19 16:44:01

麻烦上面代码最后的cpp删掉,多打了


by xyy_xxyy @ 2024-01-19 16:44:01

cik不能为0


by TryHardToBeAlive @ 2024-01-19 16:45:22

@xyy_xxyy 嗯两种改法都行,反正最后保证while能够及时退出来就好


by xyy_xxyy @ 2024-01-19 16:46:24

谢谢


|