C++ 查找最大值和最小值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22775492/
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
Find Max & Min values
提问by ChaseHardin
I am trying to find the minimum and maximum value. I am running into following two issues:
我试图找到最小值和最大值。我遇到以下两个问题:
- When entering only negative numbers, the max value will be equal to 0, not the input's maximum value.
- I have a similar issue when entering only positive numbers. Basically the smallest value will be 0 not the input's value.
- 仅输入负数时,最大值将等于 0,而不是输入的最大值。
- 仅输入正数时,我遇到了类似的问题。基本上最小值将是 0 而不是输入的值。
Any tips would be appreciated.
任何提示将不胜感激。
#include <iostream>
using namespace std;
int main()
{
int input;
int max = 0;
int min = 0;
cout << "Enter number: ";
while (input != -1)
{
cin >> input;
if(input != -1)
{
if(input > max)
{
max = input;
}
if(input < min)
{
min = input;
}
}
}
cout <<"Max: " << max << " Min " << min << endl;
}
回答by Rashad
From your code I guessed that there will be no negative number. Then do something like this:
从您的代码中,我猜想不会有负数。然后做这样的事情:
EDIT:This part from another answer will be good.
编辑:来自另一个答案的这部分会很好。
include <iostream>
using namespace std;
int main()
{
int input;
int hasInput = 0;
int max = std::numeric_limits<int>::min();
int min = std::numeric_limits<int>::max(); //#include <limits>
cout << "Enter number: ";
while (cin >> input)
{
if(input == -1) break;
hasInput = 1;
if(input > max)
{
max = input;
}
if(input < min)
{
min = input;
}
}
if(hasInput == 1)
cout <<"Max: " << max << " Min " << min << endl;
}
回答by Straw1239
At the beginning, instead of setting to 0, ask for the first number before the while loop and set min and max to that number. As a side note, using -1 as a quit condition for something which accepts numerical input might lead to problems, you might want to ask them for the number of numbers beforehand or detect non numerical input to end the program.
一开始,不是设置为 0,而是在 while 循环之前询问第一个数字,并将 min 和 max 设置为该数字。作为旁注,使用 -1 作为接受数字输入的东西的退出条件可能会导致问题,您可能需要事先询问他们的数字数量或检测非数字输入以结束程序。
EDIT: example code:
编辑:示例代码:
int main(int argc, char** argv)
{
int input;
bool hasInput = false;
cout << "Enter number: ";
cin >> input;
int max = input;
int min = input;
cout << "Enter number: ";
while (cin >> input)
{
if(input == -1) break;
hasInput = true;
if(input > max)
{
max = input;
}
if(input < min)
{
min = input;
}
cout << "Enter number: ";
}
if(hasInput)
cout <<"Max: " << max << " Min " << min << endl;
}
Forgot to check if the first input is -1... im sure you can figure that one out.
忘记检查第一个输入是否为 -1 ......我相信你可以弄清楚。
回答by yizzlez
Just like @Turix said, the problem is because of your initial max
and min
values. Your max
value should be the smallest possible integer value so that anythingis greater than it, similarly your min
should be the largest possible integer value. With this in hand, intialize your variables like this:
就像@Turix 所说的,问题出在你的初始值max
和min
值上。您的max
值应该是最小可能的整数值,以便任何大于它的值,同样您min
应该是最大可能的整数值。有了这个,像这样初始化你的变量:
int max = std::numeric_limits<int>::min();
int min = std::numeric_limits<int>::max(); //#include <limits>