比较多的数字找到最小和最大 [英] Comparing many numbers to find minimum and maximum

查看:113
本文介绍了比较多的数字找到最小和最大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要保持它的简单可以说我有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.

这篇关于比较多的数字找到最小和最大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆