C++如何得到最大和最小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16743782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ How to get the largest and smallest
提问by George
This is a simple introduction course question, it asked to write a program that ask user to input 3 numbers, and determine the largest and smallest number. Only using if statement.I thought about it wrote something like this, but is it possible to only use 3 comparisons, or less?I think when y > largest, it also tells us something else as well?
这是一道简单的入门课程问题,要求编写一个程序,让用户输入3个数字,并确定最大和最小的数字。仅使用 if 语句。我想过它写了这样的东西,但是否可以只使用3次或更少的比较?我想 when y > largest,它也告诉我们其他的东西?
This is what I tried so far: which required 4 comparisons.
这是我到目前为止所尝试的:需要 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;
回答by Luchian Grigore
The issue with your code is that you toss out a lot of information. In "challenges" such as these, you have to make the most of what you have. So when you say, for example
你的代码的问题是你扔掉了很多信息。在诸如此类的“挑战”中,您必须充分利用您所拥有的。所以当你说,例如
if (y > largest)
don't just treat the truecase. Also try to reason about the case when the condition doesn't hold.
不要只处理true案件。当条件不成立时,还要尝试对这种情况进行推理。
if ( x < y )
{
smallest = x;
biggest = y;
}
else
{
smallest = y;
biggest = x;
}
if ( z < smallest )
smallest = z;
else if ( z > biggest )
biggest = z;
This contains only 3 comparisons.
这仅包含 3 个比较。
回答by SmartGuy
The question is finding the largest or smallest by only if else statements, and we have three variables to use, so we only need two comparisons.
问题是仅通过 if else 语句找到最大或最小,并且我们有三个变量要使用,因此我们只需要进行两次比较。
{
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.
回答by Ilya Kogan
Why are you checking if (y < smallest)? At this point in the flow, smallestmust be x, but you've already checked if y > xin the first condition (if (y > largest)), so the third condition is redundant.
你为什么要检查if (y < smallest)?在流程的这一点上,smallestmust be x,但是您已经检查了是否y > x在第一个条件 ( if (y > largest)) 中,因此第三个条件是多余的。
回答by P. Tristan
int a; int b; int c;
cin >> a >> b >> c;
if ( a > b && b > c ){ cout << a << " MAIXM \n" << b << " MEDIU \n" << c << " MINIM \n"; }
if ( b > a && a > c ){ cout << b << " MAIXM \n" << a << " MEDIU \n" << c << " MINIM \n"; }
if ( c > b && b > a ){ cout << c << " MAIXM \n" << b << " MEDIU \n" << a << " MINIM \n"; }
if ( a > c && c > b ){ cout << a << " MAIXM \n" << c << " MEDIU \n" << b << " MINIM \n"; }
if ( b > c && c > a ){ cout << b << " MAIXM \n" << c << " MEDIU \n" << a << " MINIM \n"; }
if ( c > a && a > b ){ cout << c << " MAIXM \n" << a << " MEDIU \n" << b << " MINIM \n"; }
回答by Timothy Shields
In general you can determine a sort for three numbers x, y, and zusing at most 3 comparisons:
一般来说,你可以确定排序为三个数字x,y和z最多使用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
So getting the min can also be done with 3 comparisons.
因此,也可以通过 3 次比较来获得最小值。
You can get the min in 2 comparisons by doing:
您可以通过执行以下操作获得 2 次比较中的最小值:
m = x;
m = min(m,y);
m = min(m,z);
where min(a,b)is a < b ? a : b.
哪里min(a,b)是a < b ? a : b。
In general you can get the min of N numbers using N - 1 comparisons.
通常,您可以使用 N - 1 次比较来获得 N 个数字的最小值。
回答by Hyman Smother
I find this easiest to understand for you.
我觉得这对你来说最容易理解。
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;
}
回答by SirGuy
This one is just for fun and I believe the myabsfunction is actually supposed to be undefined behaviour, but I've only ever seen places that it works as expected.
这只是为了好玩,我相信该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;
}
Which outputs 0 2correctly with 0 comparisons in total.
哪个输出0 2正确,总共有 0 个比较。

