大佬们看一下,我自己运行就是对的,在网站上就超时了

P1319 压缩技术

l2975572920 @ 2023-01-28 14:35:42

#include<stdio.h>
int main() {
    int m, n,x=0,y=0;
    scanf("%d", &n);
    while (x <= n * n) {
        scanf("%d", &m);
        y++;
        for (int i = 0; i < m; i++) {
            printf("%d", (y + 1) % 2);
            x++;
            if (x == n) {
                printf("\n");
                x = 0;
            }
        }
    }
}

by 大眼仔Happy @ 2023-01-28 14:50:52

@l2975572920 你的程序也没有结束啊。。。


by codejiahui @ 2023-01-28 14:56:39

return 0掉哪儿了??????


by Dian_ed @ 2023-01-28 15:19:15

@codejiahui 请问一定要加return 0吗?我平常没有这个习惯,不知道会不会导致程序超时?


by 鏡音リン @ 2023-01-28 15:21:38

@2022TYSC0784 不需要加,对程序没有任何影响


by ChickenDrinkingMilk @ 2023-01-28 15:22:05

@2022TYSC0784 return 0在这里的作用是跳出循环,你程序没结束


by codejiahui @ 2023-01-28 15:32:08

@2022TYSC0784 这个代码必须加,其他的建议加上,CSP要求应该是不加0分(当然我可能记错了,不过最好养成习惯)


by l2975572920 @ 2023-01-28 16:01:51

@大眼仔Happy 最后的x=0;不小心弄了个死循环?


by l2975572920 @ 2023-01-28 16:04:52

#include"stdio.h"
int main() {
    int m, n, x = 0, y = 0;
    scanf_s("%d", &n);
    while (x < n * n) {
        scanf_s("%d", &m);
        y++;
        for (int i = 0; i < m; i++) {
            printf("%d", (y + 1) % 2);
            x++;
            if (x % n == 0) 
                printf("\n");
        }
    }
}

找到问题了,这个是对的,那个x和循环的x冲突了,不小心弄了个死循环


|