简体   繁体   English

它也不是最大/最小代码我如何找出这个代码?

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

get a program that gets (three integers ) and display the value that is neither max nor min?得到一个程序,它得到(三个整数)并显示既不是最大值也不是最小值的值?

I tried to think of a way to get a number between max and min.我试图想出一种方法来获得最大值和最小值之间的数字。 My input is x,y,z which are (three integers ) how to get this number which is neither max nor 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;
}

will I know that this is not the right code but I know that the path is somewhere close.我会知道这不是正确的代码,但我知道路径很近。

get a number between max and min.得到一个介于最大值和最小值之间的数字。 input (three integers )输入(三个整数)

This is essentially the median of the three numbers.这基本上是三个数字的中位数。

A simple solution can be to take the elements in an array, sort them and then pick the middle element, like so,一个简单的解决方案是获取数组中的元素,对它们进行排序,然后选择中间元素,如下所示,

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

I am assuming that all the numbers are distinct otherwise there is no number which is neither min nor max.我假设所有数字都是不同的,否则没有数字既不是最小值也不是最大值。 For example, of the three numbers are 3, 3 and 3, 3 is both the min and max.例如,在三个数字中,3、3 和 3,3 既是最小值又是最大值。 There is no solution in such a case.在这种情况下没有解决方案。

With this code you compare the first 2 variable to see witch of them is the max value and after that just compare with the other variable使用此代码,您可以比较前 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM