C++ - 从 3 个数字中找到最大的 2 个
C++ - Finding greatest 2 numbers out of 3
我想从 3 个数字 A、B 和 C 中找到最大和第二大的数字。
我知道我可以像这样使用 max() 函数:max(a,max(b,c));你怎么会找到第二伟大的?
算法将如下所示:
greatest = std::max(a, secondGreatest = std::max(b, c));
secondGreatest = std::min(std::max(std::min(b, c), a), secondGreatest);
以下是断言中大多数组合的工作列表:
#include <iostream>
#include <cassert>
int main()
{
int a = 5, b = 10, c = 15;
int greatest = 0, secondGreatest = 0;
greatest = std::max(a, secondGreatest = std::max(b, c));
assert(secondGreatest = std::min(std::max(std::min(b, c), a), secondGreatest) == 10);
a = 10, b = 5, c = 15;
greatest = std::max(a, secondGreatest = std::max(b, c));
assert(secondGreatest = std::min(std::max(std::min(b, c), a), secondGreatest) == 10);
a = 15, b = 5, c = 10;
greatest = std::max(a, secondGreatest = std::max(b, c));
assert(secondGreatest = std::min(std::max(std::min(b, c), a), secondGreatest) == 10);
a = 15, b = 10, c = 5;
greatest = std::max(a, secondGreatest = std::max(b, c));
assert(secondGreatest = std::min(std::max(std::min(b, c), a), secondGreatest) == 10);
a = 5, b = 15, c = 10;
greatest = std::max(a, secondGreatest = std::max(b, c));
assert(secondGreatest = std::min(std::max(std::min(b, c), a), secondGreatest) == 10);
a = 10, b = 15, c = 5;
greatest = std::max(a, secondGreatest = std::max(b, c));
assert(secondGreatest = std::min(std::max(std::min(b, c), a), secondGreatest) == 10);
a = 15, b = 5, c = 5;
greatest = std::max(a, secondGreatest = std::max(b, c));
assert(secondGreatest = std::min(std::max(std::min(b, c), a), secondGreatest) == 5);
return 0;
}
在我看来,最简单的解决方案,但可能有点矫枉过正,就是将它们放在数组或向量中并进行排序。
通过这种方式,您可以访问所需的任何内容,并且可以轻松地将更多数字添加到组合中。您也不必跟踪所有if
。
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
static const int size = 3;
int arr[size] = { 3, 1, 2 };
sort(arr, arr+size);
cout << "Lowest: " << arr[0] << endl; // Prints out 1
cout << "Middle: " << arr[1] << endl; // Prints out 2
cout << "Highest: " << arr[2]; // Prints out 3
return 0;
}
或者,您可以自己遍历数组并使用 O(n) 循环查找第二大数字,这比排序更有效。
这是一个函数,可用于从数组中获取第二大数字(从此答案解释)
#include <iostream>
#include <limits>
#define int_min numeric_limits<int>::min()
using namespace std;
int SecondGreatest(int arr[], int count){
int largest = int_min,
second = int_min;
for(int i = 0; i < count; i++){
if(arr[i] > largest){
second = largest;
largest = arr[i];
}else if(arr[i] > second){
second = arr[i];
}
}
return second;
}
int main(){
static const int size = 4;
int arr[size] = { 6, 5, 8, 10 };
cout << "Second largest: " << SecondGreatest(arr, size); //Prints out 8
return 0;
}
这是一个参考。
如果你不需要最大的两个之间的顺序,那么找到最大和第二大是没有必要的。我们只需要找到三个中最小的一个,就知道哪两个最大。这可以通过使用通用算法来完成 查找最少并将其删除 .
std::list<int> numbers(...) // put three your numbers here.
std::list<int>::iterator min = std::min_element(std::begin(v), std::end(v));
numbers.erase(min);
然后你的两个数字在numbers
.
如果您需要订单,只需对其进行排序即可。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 打印数字图案
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 如何检查一个c++字符串中有多少相同的字符/数字
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 将数字打印成文字
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 在将数字随机生成为数组期间从内存输出随机数的数组