使用不同的编译器时,我会得到不同的输出

发布于 2025-01-25 04:12:07 字数 1084 浏览 1 评论 0原文

我试图解决使用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;
}

编辑:对不起,我将变量名称更改为英语清晰理解,但我忘记了toplamsum)和toplamlarsums)。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁许谁一生繁华 2025-02-01 04:12:07

假设您将一些变量名称转换为非土耳其语说话者的英语,则变量sumpoplam)是未经初始化的,导致未定义的遗嘱。不确定行为的常见症状是不同系统 /编译器上的行为不同。

请注意,您可以通过仅搜索数组中的最小值和最大值来简化代码:

#include <stdio.h>

void miniMaxSum(const int *a, int sum) {
    int min = a[0], max = a[0];
    for (int j = 1; j < 5; j++) {
        if (sums[j] < min)
            min = sums[j];
        if (sums[j] > max)
            max = sums[j];
    }
    printf("%d %d\n", sum - max, sum - min);
}

int main() {
    int numbers[5] = { 0, 0, 0, 0, 0 };
    int sum = 0;
    for (int i = 0; i < 5; i++) {
        scanf("%d ", &numbers[i]);
        sum += numbers[i];
    }

    miniMaxSum(numbers, sum);

    return 0;
}

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:

#include <stdio.h>

void miniMaxSum(const int *a, int sum) {
    int min = a[0], max = a[0];
    for (int j = 1; j < 5; j++) {
        if (sums[j] < min)
            min = sums[j];
        if (sums[j] > max)
            max = sums[j];
    }
    printf("%d %d\n", sum - max, sum - min);
}

int main() {
    int numbers[5] = { 0, 0, 0, 0, 0 };
    int sum = 0;
    for (int i = 0; i < 5; i++) {
        scanf("%d ", &numbers[i]);
        sum += numbers[i];
    }

    miniMaxSum(numbers, sum);

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文