C++ 如何以最少的比较获得3个值中的最大值和最小值?

C++ 如何以最少的比较获得3个值中的最大值和最小值?,c++,if-statement,C++,If Statement,这是一个简单的介绍课程问题。我必须编写一个程序,要求用户输入3个数字,并确定最大和最小的数字 我只需要使用if语句 这就是我到目前为止所尝试的:需要4次比较 int x, y, z; int smallest, largest; cout << "Please enter 3 numbers to compare: " ; cin >> x >> y >> z; smallest = x; largest = x; if

这是一个简单的介绍课程问题。我必须编写一个程序,要求用户输入3个数字,并确定最大和最小的数字

我只需要使用
if
语句

这就是我到目前为止所尝试的:需要4次比较

int x, y, z;
int smallest, largest; 
cout << "Please enter 3 numbers to compare: " ;
cin >> x >> y >> z;

smallest = x;
largest = x;

if (y > largest) 
        largest = y;
if (z > largest)
        largest = z;
if (y < smallest)
        smallest = y;
if (z < smallest)
        smallest = z;

cout << "largest: " << largest << ", and smallest: " << smallest << endl;   
{
    int valueOne,
    valueTwo,
    valueThree,
    smallest;

//User input for valueOne, valueTwo, valueThree.

smallest = valueOne;

if (smallest < valueTwo)
{
smallest = valueTwo;
}
if (smallest < valueThree)
{
smallest = valueThree;
}

//No matter what happens, smallest will have the smallest value now.

//Use >, rather than <, and "largest" rather than "smallest" for finding largest value.

//With this logic, you always will have one less comparison than the total number or variables to compare

//i.e. 7 variables means 6 comparisons.

//This contains only 2 comparisons.
intx,y,z;
int最小,最大;
cout>x>>y>>z;
最小=x;
最大=x;
如果(y>最大值)
最大=y;
如果(z>最大值)
最大=z;
if(y<最小值)
最小=y;
if(z<最小值)
最小=z;

cout通常,您最多可以使用3个比较来确定三个数字的排序:

if (x < y)
    if (y < z)
       //x,y,z -> x min
    else if (x < z)
       //x,z,y -> x min
    else
       //z,x,y -> z min
else
    if (z >= x)
       //y,x,z -> y min
    else if (z >= y)
       //y,z,x -> y min
    else
       //z,y,x -> z min
其中
min(a,b)
a



通常,您可以使用N-1比较来获得N个数字的最小值。

为什么要检查
如果(y
?在流程中的这一点上,
最小的
必须是
x
,但是您已经检查了第一个条件中的
y>x
if(y>最大)
),因此第三个条件是多余的。

代码的问题是您丢弃了很多信息。在这样的“挑战”中,你必须充分利用你所拥有的。例如,当你说

int x, y, z;
int smallest, largest; 
cout << "Please enter 3 numbers to compare: " ;
cin >> x >> y >> z;

smallest = x;
largest = x;

if (y > largest) 
        largest = y;
if (z > largest)
        largest = z;
if (y < smallest)
        smallest = y;
if (z < smallest)
        smallest = z;

cout << "largest: " << largest << ", and smallest: " << smallest << endl;   
if (y > largest) 
{
    int valueOne,
    valueTwo,
    valueThree,
    smallest;

//User input for valueOne, valueTwo, valueThree.

smallest = valueOne;

if (smallest < valueTwo)
{
smallest = valueTwo;
}
if (smallest < valueThree)
{
smallest = valueThree;
}

//No matter what happens, smallest will have the smallest value now.

//Use >, rather than <, and "largest" rather than "smallest" for finding largest value.

//With this logic, you always will have one less comparison than the total number or variables to compare

//i.e. 7 variables means 6 comparisons.

//This contains only 2 comparisons.
不要只处理
真实的案例。当条件不成立时,也试着对该案例进行推理

if ( x < y )
{
    smallest = x;
    biggest = y;
}
else
{
    smallest = y;
    biggest = x;
}

if ( z < smallest )
   smallest = z;
else if ( z > biggest )
   biggest = z;
if(x最大值)
最大=z;

这只包含3个比较。

我发现这对您来说最容易理解

int x, y, z;
int smallest, largest; 
cout << "Please enter 3 numbers to compare: " ;
cin >> x >> y >> z;

smallest = x;
largest = x;

if (y > largest) 
        largest = y;
if (z > largest)
        largest = z;
if (y < smallest)
        smallest = y;
if (z < smallest)
        smallest = z;

cout << "largest: " << largest << ", and smallest: " << smallest << endl;   
a = 5;
b = 10;
c = 15;

//FIND MAX
if (a >= b && a >= c)
{
   max = a;

} else
{
    if (b >= c)
        max = b
    else
        max = c;
}

//FIND MIN
if (a <= b && a <= c)
{

    min = a;
} else
{
    if (b <=c)
        min = b;
    else
        min = c;
}
{
    int valueOne,
    valueTwo,
    valueThree,
    smallest;

//User input for valueOne, valueTwo, valueThree.

smallest = valueOne;

if (smallest < valueTwo)
{
smallest = valueTwo;
}
if (smallest < valueThree)
{
smallest = valueThree;
}

//No matter what happens, smallest will have the smallest value now.

//Use >, rather than <, and "largest" rather than "smallest" for finding largest value.

//With this logic, you always will have one less comparison than the total number or variables to compare

//i.e. 7 variables means 6 comparisons.

//This contains only 2 comparisons.
a=5;
b=10;
c=15;
//找到MAX
如果(a>=b&&a>=c)
{
max=a;
}否则
{
如果(b>=c)
最大值=b
其他的
max=c;
}
//找敏

如果(a这个函数只是为了好玩,我相信
myabs
函数实际上应该是未定义的行为,但我只见过它按预期工作的地方

    double myabs(double x)
    {
        int64_t * p = (int64_t*)&x;
        //clear sign bit
        *p &= 0x7fffffffffffffff;
        return x;
    }

    int main()
    {
        double x = 0, y = 1, z = 2;
        //find max/min of first two numbers
        double min = (myabs(x+y)-myabs(x-y))/2;
        double max = (myabs(x+y)+myabs(x-y))/2;
        //find max/min of previous max/min and third number
        min = (myabs(min+z) - myabs(min-z))/2;
        max = (myabs(max+z) + myabs(max-z))/2;
        std::cout << min << ' ' << max << std::endl;
        return 0;
    }
double myabs(双x)
{
int64_t*p=(int64_t*)&x;
//清除符号位
*p&=0x7fffffffffffff;
返回x;
}
int main()
{
双x=0,y=1,z=2;
//查找前两个数字的最大/最小值
双最小=(myabs(x+y)-myabs(x-y))/2;
双最大值=(myabs(x+y)+myabs(x-y))/2;
//查找上一个最大/最小值和第三个数字的最大/最小值
min=(myabs(min+z)-myabs(min-z))/2;
max=(myabs(max+z)+myabs(max-z))/2;

std::cout问题是仅通过if-else语句查找最大值或最小值,我们有三个变量要使用,所以我们只需要两个比较

int x, y, z;
int smallest, largest; 
cout << "Please enter 3 numbers to compare: " ;
cin >> x >> y >> z;

smallest = x;
largest = x;

if (y > largest) 
        largest = y;
if (z > largest)
        largest = z;
if (y < smallest)
        smallest = y;
if (z < smallest)
        smallest = z;

cout << "largest: " << largest << ", and smallest: " << smallest << endl;   
{
    int valueOne,
    valueTwo,
    valueThree,
    smallest;

//User input for valueOne, valueTwo, valueThree.

smallest = valueOne;

if (smallest < valueTwo)
{
smallest = valueTwo;
}
if (smallest < valueThree)
{
smallest = valueThree;
}

//No matter what happens, smallest will have the smallest value now.

//Use >, rather than <, and "largest" rather than "smallest" for finding largest value.

//With this logic, you always will have one less comparison than the total number or variables to compare

//i.e. 7 variables means 6 comparisons.

//This contains only 2 comparisons.
{
int valueOne,
第二,
第三,
最小的;
//用户输入值一、值二、值三。
最小值=1;
if(最小值<值二)
{
最小值=2;
}
if(最小值<三)
{
最小值=三;
}
//无论发生什么,最小值现在都将具有最小值。
//使用>,而不是
inta;intb;intc;
cin>>a>>b>>c;

如果(a>b&&b>c){我能肯定z是否是执行的三个比较中最大的一个。@LuchianGrigore我不知道它是否重要,但如果它只是
if语句
,没有
else语句
。它也可以在三个比较中完成吗?@George the
else
if
语句的一部分。@LuchianGrigore我知道,但那是因为这是一个比较stion出现在本书教授
else
语句之前的章节中,我只是想知道。@George:如果您的练习仅限于此,请将第一个
else
部分放在
If
之前(因此每次都会执行它,但是如果
If
条件为true,则会覆盖值)提示:bubblesort只能进行三次比较,并会告诉您最小值和最大值。实际上,bubblesort通常进行交换,因此它比一些答案更复杂。
auto[最小,最大]=std::minmax({x,y,z})
仅承诺“不超过4”比较当然,只要从
最大的=y
开始,而不是从
最大的=x
开始,然后看看它会带你去哪里…只有当
长的
是64位时,它才会起作用。不会有太多地方起作用。例如,我从未见过这样的地方example@kotlomoy
long
在Linux上是64位的,但在Windows上不是。对于不同的数据有不同的定义您可以使用的Windows类型。但我不确定它是什么。@kotlomoy将
long
更改为
stdint.h
中定义的
int64_t
。这应该也能使它在Windows上工作(但我无法测试)最好是<代码> <代码>,而不是<代码> <代码>,因为这是一个C++问题。<代码> <代码>实际上是C++ 11,实际上这是我尝试的第一件事。