-2

我想从 3 个数字 A、B 和 C 中找出最大的和次大的。

最重要的是,我知道我可以像这样使用 max() 函数: max(a,max(b,c)); 你会如何找到第二大?

4

3 回答 3

2

在我看来,最简单的解决方案,但也许有点矫枉过正,就是把它们放在一个数组或一个向量中并排序。

通过这种方式,您可以访问您正在寻找的任何内容,并且可以轻松地将更多数字添加到组合中。您也不必跟踪所有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) 循环查找第二大数字,这比排序更有效。

这是一个可用于从数组中获取第二大数字的函数(从this answer解释)

#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;
}

这是一个参考

于 2013-10-24T02:24:11.483 回答
1

该算法将如下所示:

    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;
}
于 2013-10-24T03:23:52.337 回答
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

如果您需要订单,请对其进行排序。

于 2013-10-24T03:37:31.260 回答