C++ - 从 3 个数字中找到最大的 2 个

C++ - Finding greatest 2 numbers out of 3

本文关键字:数字 C++      更新时间:2023-10-16

我想从 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.

如果您需要订单,只需对其进行排序即可。