也不是最大/最小代码,我如何找出此代码?

It is nor max/min code how do i figure out this code?

本文关键字:代码 何找出      更新时间:2023-10-16

获取一个程序,它得到(三个整数(并显示的值既不是max也不是min?

我试图想出一种方法来获取最大和最小之间的数字。我的输入是x,y,z,它们是(三个整数(如何获得这个既不是最大值也不是最小的数字?

int x,y,z,max,min,between;
cout<<"enter 3 integer values n";
cin>>x>>y>>z,between;
if (x>y&&x>z)
x=max;
else if (x<y&&x<z)
x=min;
else if(y>z&&y>x)
y=max;
else if (y<z&&y<x)
y=min;
else if(!max==between&& !min==between)
cout<<"not max or min is "<<between<<endl;
return 0;
}

我会知道这不是正确的代码,但我知道路径在某个地方.

得到一个介于最大值和最小值之间的数字。 输入(三个整数(

这基本上是三个数字的中位数。

一个简单的解决方案可以是将数组中的元素,对它们进行排序,然后选择中间的元素,如下所示,

int elements[3] = ... // contains x,y,z
std::sort(std::begin(elements), std::end(elements)); // sorts the elements
elements[1] // pick the middle element

我假设所有数字都是不同的,否则没有既不是最小也不是最大的数字。例如,在三个数字中,3、3 和 3 是最小值和最大值。在这种情况下没有解决方案。

使用此代码,您可以比较前 2 个变量以查看它们的最大值,然后只需与另一个变量进行比较

if (x < y) 
{
if (z < x)  
{
return x;
}
else if(z < y) {
return z;
}
else {
return y;
}
}
else if (y < z) 
{
if (z < x) {
return z;
}
else {
return y;
}
}