0

我一直在尝试 Codeforces 提交页面上可用的不同编译器,但没有一个不同的编译器提供诸如 Code::Block 之类的输出

问题链接:https ://codeforces.com/problemset/problem/282/A

这是我的代码:

#include <stdio.h>
#include <string.h>

int main() {
int count = 0, i = 0, final;
int x = 0;
char strg[3];

scanf("%d", &count);

for (i = 0; i < count; i++){
    scanf("%s", &strg[0], &strg[1], &strg[2]);
    if ((strcmp(strg,"x++") == 0) || (strcmp(strg,"++x") == 0)){
        x = x+1;
    } if ((strcmp(strg,"x--") == 0) || (strcmp(strg,"--x") == 0)){
        x = x-1;
    }
}
printf("%d", x);

}

提交页面输出:“错误答案第一个数字不同 - 预期:'1',找到:'0'” 但是,Code::Blocks 打印正确的值是“1”。

Codeforces 提交:

Codeforces 编译器输出

程序运行输出:

从代码块运行代码

4

1 回答 1

1

strg[3]太短了,无法容纳 2 个字符的字符串。这是一种未定义的行为。

由于它是未定义的,它在某些环境中工作,而在另一个环境中不起作用。

于 2020-06-21T17:15:49.843 回答