排序三个数字的简单方法

是否有更简单,更好的方法来解决这个问题,因为

  1. 我使用了太多变量。
  2. 我使用了很多if else语句
  3. 我是用蛮力方法做到的

编写一个接收三个整数作为输入的程序,并按递增顺序输出数字。
不要使用循环/数组。

 #include  main(){ int no1; int no2; int no3; int sto; int hi; int lo; printf("Enter No. 1: "); scanf("%d", &no1); printf("Enter No. 2: "); scanf("%d", &no2); printf("Enter No. 3: "); scanf("%d", &no3); if (no1>no2) { sto=no1; lo=no2; } else { sto=no2; lo=no1; } if (sto>no3) { hi=sto; if(lo>no3){ sto=lo; lo=no3; }else { sto=no3; } }else hi=no3; printf("LOWEST %d\n", lo); printf("MIDDLE %d\n", sto); printf("HIGHEST %d\n", hi); getch(); } 

 if (a > c) swap(a, c) if (a > b) swap(a, b) //Now the smallest element is the first one. Just check the 2-nd and 3-rd if (b > c) swap(b, c); 

注意:交换更改两个变量的值。

调用三个变量xyz ,然后:

 if (x > y) swap(x, y); if (y > z) swap(y, z) if (x > y) swap(x, y); 

swapfunction留给读者练习。 提示:您可能必须使用指针。

 #include  #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) int main(){ int a, b, c; int hi; int lo; printf("Enter No. 1: "); scanf("%d", &a); printf("Enter No. 2: "); scanf("%d", &b); printf("Enter No. 3: "); scanf("%d", &c); lo = min(min(a, b), c); hi = max(max(a, b), c); printf("LOWEST %d\n", lo); printf("MIDDLE %d\n", a+b+c-lo-hi); printf("HIGHEST %d\n", hi); getchar(); } 

提示:如果你有3个数字,a,b和c,min(a,min(b,c))是最小的,max(a,max(b,c))是最大的,并给出最小的和最大的数字,应该很容易找到第三个。

是的,有一个更好的方法,但你需要使用循环和数组。

也许,对于入门课程,您的答案就是他们正在寻找的答案。

有方法可以使用for / while(递归,转到等)进行循环。 以及如何在没有索引的情况下获得类似数组的方法( int *ptr = malloc (3 * sizeof(int)) ,然后用*(ptr+index) )。 但是,我发现很难想到这就是他们想要的。

要查找3个值的最小值中间值和最大值 ,可以使用三元运算符。 您可以在代码的主体中完成所有工作,也可以将minof3midof3maxof3计算分成可重用的函数。

minmax的情况下,您只需进行3次可能的比较中的2次,然后返回结果的比较。 在mid的情况下,你做同样的事情,但是计算3个值的最小值和最大值,然后检查所有3对照最小值最大值 ,以便找到既不是最小值也不是最大值的值 。 (通过将min和max值声明为变量并在那里进行消除,您可以在代码的主体中执行此部分而无需其他function)。

将各个部分组合在一起,您可以执行类似于以下操作的操作,它将前3个参数作为要排序的值(如果未指定所需值,则使用默认值99,231,8)

 #include  #include  /** direct ternary comparison of 3 values */ long minof3 (long a, long b, long c) { long x = a < b ? a : b, y = a < c ? a : c; return x < y ? x : y; } long maxof3 (long a, long b, long c) { long x = a > b ? a : b, y = a > c ? a : c; return x > y ? x : y; } long midof3 (long a, long b, long c) { long x = minof3 (a, b, c), z = maxof3 (a, b, c), y = a == x ? b : a; return y == z ? c : y; } int main (int argc, char **argv) { long x = argc > 1 ? strtol (argv[1], NULL, 10) : 99, y = argc > 2 ? strtol (argv[2], NULL, 10) : 231, z = argc > 3 ? strtol (argv[3], NULL, 10) : 8; printf ("\n sorted values : %ld, %ld, %ld\n", minof3 (x, y, z), midof3 (x, y, z), maxof3 (x, y, z)); return 0; } 

示例使用/输出

 $ ./bin/sort3 sorted values : 8, 99, 231 $ ./bin/sort3 -23 -281 1031 sorted values : -281, -23, 1031 

(是的,我知道这是一个旧post,但鉴于最近关于隐藏在swap函数后面的代码的评论,完整的例子是有序的)。

如果要将值排序为新的外部变量,实际上可以在没有临时值的情况下进行交换:

 void sort(int a, int b, int c, int *min, int *mid, int *max) { min = a; mid = b; max = c; if (min > mid) { mid = a; min = b; } if (mid > max) { max = mid; mid = c; if (min > mid) { mid = min; min = c; } } } 

这是有效的,因为最后的交换测试实际上只在第二次测试成功时才需要(否则它只是第一次测试的重复,由于我们已经对这些变量进行了排序,因此定义将失败)。

因此,我们可以跟踪每个原始变量的分配并避免交换本地变量。

我今天试图解决同样的问题。 可以在不使用任何临时变量的情况下制作此紧凑版本的代码 循环; 库函数,如swap,sort,max,min等。代码仅使用if语句并在层次结构中进行连续变换,直到检查所有可能性。

 int main() { int a, b, c; //User inputs stored in these three variables int first, second, third; //These three variables will store the sorted numbers in sequence std::cout<<"Please enter three integers : "; //User input prompt std::cin>>a>>b>>c; first = a; //Initially assuming number 'a' is smallest if (b <= a && b <= c) first = b; //Checking whether b is smallest if (c <= a && c <= b) first = c; //Checking whether c is smallest if (((a >= b && a <= c) || (a >= c && a <= b))) second = a; //Checking if a is middle number if (((b >= a && b <= c) || (b >= c && b <= a))) second = b; //Checking if b is middle number if (((c >= a && c <= b) || (c >= b && b <= a))) second = c; //Checking if c is middle number if (a >= b && a >= c) third = a; //Checking if a is the greatest if (b >= c && b >= a) third = b; //Checking if b is the greatest if (c >= a && c >= b) third = c; //Checking if c is the greatest std::cout<<"The numbers in ascending order are : "< 

如果我们寻找最小数量的比较作为获得3个元素排序的最有效解决方案,请遵循以下实现

 public static void Sort3Elements(int a, int b, int c) { if (a <= b && a <= c) //a is lowest here { if (b<=c) //a <= b <= c Console.WriteLine("{0}-{1}-{2}", a, b, c); else //a <= c <= b Console.WriteLine("{0}-{1}-{2}", a, c, b); } else if (b<=a && b<=c) //b is lowest here { if (a <= c) //b <= a <= c Console.WriteLine("{0}-{1}-{2}", b, a, c); else //b <= c <= a Console.WriteLine("{0}-{1}-{2}", b, c, a); } else //c is lowest { if (a <= b) //c <= a <= b Console.WriteLine("{0}-{1}-{2}", c, a, b); else //c <= b <= a Console.WriteLine("{0}-{1}-{2}", c, b, a); } }