比较多的数字找到最小和最大比较多、最小、数字、最大

2023-09-11 05:56:50 作者:神经病一号

要保持它的简单可以说我有3个整数(整数),我知道我能找到的最高使用是这样的:

To keep it simple lets say I have 3 whole numbers (integers) I know I can find the highest by using something like:

if(num1 > num2 && num1 > num3)
    cout << num1 << endl;
if(num2 > num1 && num2 > num3)
    cout << num2 << endl;
if(num3 > num1 && num3 > num2)
    cout << num3 << endl; 

和最低的:

if(num1 < num2 && num1 < num3)
    cout << num1 << endl;
if(num2 > num1 && num2 > num3)
    cout << num2 << endl;
if(num3 < num1 && num3 < num2)
    cout << num3 << endl;

我怎么能得到这样的事情,以解决诸如221111122121平等。 编辑:我试着远离任何prebuilt数学包括因为这是不是问题的关键在这里。

How can I get something like this to deal with equalities like 221,111,122,121. edit: Im trying to stay away from any prebuilt math includes as that is not the point here..

推荐答案

您应该考虑在一个容器中存储您的值,例如向量(我不能,如果告诉你的总是的有三个,或者您可以在不同而有所差异)。然后你可以使用的std :: min_element 的std :: max_element 这已写入和调整,以找到最小/最大距离值的序列

You should look into storing your values in a container such as vector (I can't tell if you will always have three or if you may vary in number). Then you can use std::min_element and std::max_element which are already written and tuned to find the min/max from a sequence of values.