有什么问题?
谢谢!
#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);
}
答案 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。三个整数的情况根本不需要任何循环。