【问题标题】:C++ - Finding greatest 2 numbers out of 3 [closed]C ++ - 在3个中找到最大的2个[关闭]
【发布时间】:2013-10-24 01:57:50
【问题描述】:

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

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

【问题讨论】:

  • 我已经设法通过使用 3 个 if 语句并根据 max(a,max(b,c)) 的值来做到这一点,第二大是其他两个中的最大值。还有什么比使用 3 个 if 语句更优雅的吗?
  • 那么您想要一个解决方案还是想知道是否有比您的解决方案更好的解决方案?您需要一个特定的问题才能获得答案,而“我怎样才能使这段代码变得更好”类型的问题可能会导致一个封闭的问题。
  • 你为什么不只找到最小的并假设不是最大的两个是最大的两个?
  • 也许用 "Show us" 结束(或开始)vanza 的评论会有所帮助
  • 为了清楚起见,他似乎想要一个解决方案。他到目前为止所得到的只能找到三个中最大的一个。他被困在如何找出第二大的数字。

标签: c++


【解决方案1】:

我认为最简单的解决方案,但可能有点矫枉过正,就是将它们放入数组或向量中并排序。

通过这种方式,您可以访问您要查找的任何内容,并且可以轻松地将更多数字添加到组合中。您也不必跟踪您的所有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;
}

Here's a reference.

【讨论】:

    【解决方案2】:

    算法如下:

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

    【讨论】:

      【解决方案3】:

      如果您不需要最大的两个之间的顺序,则不需要找到最大的和次大的。我们只需要找到三个中最小的就知道哪两个是最大的。这可以通过使用找到最少并删除它的通用算法来完成。

      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

      如果您需要订单,则只需排序即可。

      【讨论】:

        猜你喜欢
        • 2015-07-28
        • 2018-03-26
        • 2022-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-22
        • 2017-11-07
        • 1970-01-01
        相关资源
        最近更新 更多