使用不同的编译器时,我会得到不同的输出
我试图解决使用C的问题。但是我在不同的编译器中获得了不同的输出。首先,我尝试了GCC,没有错误,但是当我使用Clang时,输出更改。
问题:
给定五个正整数,找到可以通过将五个整数中的四个求和可以计算得出的最小值和最大值。然后将相应的最小值和最大值打印为两个空间分隔整数的单行。
示例输入:1 2 3 4 5
样本输出:10 14
10 = 1 + 2 + 3 + 3 + 4 | 14 = 2 + 3 + 4 + 4 + 5
我使用clang时的输出:1 14
这是代码:
#include <stdio.h>
void miniMaxSum(int *a, int b) {
int sums[5] = { b, b, b, b, b };
int min = *a, max = *a;
for (int j = 0; j < 5; j++) {
sums[j] -= *(a + j);
if (sums[j] < min)
min = sums[j];
if (toplamlar[j] > max)
max = sums[j];
}
printf("%d %d\n", min, max);
}
int main() {
int numbers[5] = { 0, 0, 0, 0, 0 };
int sum;
for (int i = 0; i < 5; i++) {
scanf("%d ", &numbers[i]);
toplam += numbers[i];
}
miniMaxSum(numbers, sum);
return 0;
}
编辑:对不起,我将变量名称更改为英语清晰理解,但我忘记了toplam
(sum
)和toplamlar
(sums
)。
I was trying to solve a problem with using C. But I got different outputs in different compilers. First I tried gcc and there was no mistake but when I use clang the output changed.
PROBLEM:
Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.
Sample Input: 1 2 3 4 5
Sample Output: 10 14
10 = 1 + 2 + 3 + 4 | 14 = 2 + 3 + + 4 + 5
The Output when I use clang: 1 14
Here is the code:
#include <stdio.h>
void miniMaxSum(int *a, int b) {
int sums[5] = { b, b, b, b, b };
int min = *a, max = *a;
for (int j = 0; j < 5; j++) {
sums[j] -= *(a + j);
if (sums[j] < min)
min = sums[j];
if (toplamlar[j] > max)
max = sums[j];
}
printf("%d %d\n", min, max);
}
int main() {
int numbers[5] = { 0, 0, 0, 0, 0 };
int sum;
for (int i = 0; i < 5; i++) {
scanf("%d ", &numbers[i]);
toplam += numbers[i];
}
miniMaxSum(numbers, sum);
return 0;
}
EDIT: Sorry, I changed the variables name to English sake of understanding, but I forgot the toplam
(sum
) and toplamlar
(sums
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您将一些变量名称转换为非土耳其语说话者的英语,则变量
sum
(poplam
)是未经初始化的,导致未定义的遗嘱。不确定行为的常见症状是不同系统 /编译器上的行为不同。请注意,您可以通过仅搜索数组中的最小值和最大值来简化代码:
Assuming you translated some of the variable names to English for non Turkish speakers, the variable
sum
(toplam
) is uninitialized leading to undefined behavor. A common symptom for undefined behavior is different behavior on a different system / compiler.Note that you can simplify your code by just searching for the minimum and maximum values in the array: