我想使用C语言从三个数字x,y和z中找到第二高的数字if(y> xz)然后x,if(xz || x> yy || x> z< y)然后z [英] I want to find second highest number from the three numbers x,y and z using C language if(y>xz) then x, if(xz||x>yy||x>z<y) then z

查看:165
本文介绍了我想使用C语言从三个数字x,y和z中找到第二高的数字if(y> xz)然后x,if(xz || x> yy || x> z< y)然后z的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我删除了空标签,然后猜测是什么,没有其他内容,这意味着没有问题.

I removed the empty tags, and guess what, there was no other content, meaning there was no question.

推荐答案

要做的就是对它们进行排序.

It may seem overkill, but the first thing to do is to sort them.

#define swap(x, y) do {int tmp; tmp = x; x = y; y = tmp; } while (0)
int getMiddleNumber(int a, int b, int c)
   {
   if (b < a) swap(b, a);
   if (c < b) swap(c, b);
   if (b < a) swap(b, a);
   return b;
   }


然后,您所要做的就是使用b-它在中间.
(此代码从此处 [


同意,但是已经有一段时间了,因为我使用了直接C语言,而且我不记得我将QuickC磁盘放在哪里了...:laugh:


Then all you have to do, is use b - it is in the middle.
(this code pinched from here[^])


"Using macros like that (while convenient) is generally frowned on. You can''t debug a macro, so you have to hope your code is right."


Agreed, but it''s been quite a while since I used straight C and I couldn''t remember where I put my QuickC disks... :laugh:

void swap(int *x, int* y)
   {
   int tmp; 
   tmp = *x; 
   *x = *y; 
   *y = tmp;
   }
int getMiddleNumber(int a, int b, int c)
   {
   if (b &lt; a) swap(&b, &a);
   if (c &lt; b) swap(&c, &b);
   if (b &lt; a) swap(&b, &a);
   return b;
   }


我不建议排序.在这种情况下,它可能会运行良好.但是随着列表的增加,复杂性也随之增加.最好的使用方法是使用两个变量,一个存储最大的变量,另一个存储第二大变量.然后从头到尾遍历数组,并将其值与这两个变量进行比较,最后您将在第二个变量中获得第二大变量.
I would not suggest sorting. In this case it might work well. But as the list increases, the complexity increases. The best way to use is to use two variables, one to store the largest, and the other to store the second largest. Then traverse through the array from first to last and compare its value with those two variables and at last you''ll get the second largest variable in the second variable.


这篇关于我想使用C语言从三个数字x,y和z中找到第二高的数字if(y> xz)然后x,if(xz || x> yy || x> z&lt; y)然后z的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆