输出在所有整数值中仍然没有逻辑

时间:2012-03-25 09:45:37

标签: c

好吧我正在做这个功课,但我仍然没有看错...当我跑(编译后没有错误的gcc)似乎工作正常......但是当我把输入,即“ 254 34 199“输出返回:”有两个相等的数字。再试一次“......根本不是逻辑。

有什么问题?

谢谢!

#include<stdio.h>

int main()
{

    puts("Enter three numbers separated by a space to determine what is the greatest, what is the one in the middle and what is the lowest.");

    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);

        int imax(int a, int b) {return a < b ? b : a;}
        int imin(int a, int b) {return a < b ? a : b;}

    int high = imax(imax(a, b), c);
    int low = imin(imin(a, b), c);

    int mid(int a, int b, int c) {
    if (a<b && a>c) return a;
    else if (b<a && b>c) return c;
    else return b;
    }

    if (high == mid && mid == low) puts("All of the numbers are equal. Try again");

    else if (high == mid || high == low || mid == low) puts("There's two equal numbers. Try again"); /* This else if makes me crazy cause is not logic with a lot of combinations of three numbers! */

    else printf("The greatest are %d, the middle are %d and the lowest are %d\n", high, mid, low);
}

2 个答案:

答案 0 :(得分:1)

您正在以错误的方式计算中间数字,例如,如果我们将您给出的值(254 34 199)放入计算中:

int mid = imax(imax(254, 34), imin(34, 199));

我们得到:

int mid = imax(254, 34);

是:

int mid = 254;

而不是199。

答案 1 :(得分:0)

Binyamin已经指出了这个问题。这是一个指向另一种可能更好的解决方案的指针:bubble sort。三个整数的情况根本不需要任何循环。