【问题标题】:How do I order a set of integers from smallest to greatest without if statements?如何在没有 if 语句的情况下从最小到最大排序一组整数?
【发布时间】:2020-11-19 07:23:51
【问题描述】:

我目前遇到一个 C++ 问题,该问题要求我对五个输入的整数从最小到最大进行排序。我尝试过使用 if 语句来做到这一点,但列出所有可能的组合似乎效率太低了。我也尝试过 min 和 max 函数,但这些函数只适用于最小和最大的两个整数。对它们进行排序的最快方法是什么?

#include <iostream>
using namespace std;

int main() {

    cout << "Enter five integers. \n\n>";
    int a, b, c, d, e;
    cin >> a >> b >> c >> d >> e;

    int first = min({a, b, c, d, e});
    int fifth = max({a, b, c, d, e});

    cout << first << fifth << "\n"; //This is where I want to output the middle three integers in order

    cout << "\n";
    return 0;
}

到目前为止,这是我的代码。 ^

【问题讨论】:

  • 放入标准容器,传给std::sort?
  • 你应该使用数组和std::sort
  • 我总是对fastest这个形容词很感兴趣。像这样使用与速度无关。这意味着使用最少的代码,或者没有任何我目前不理解的东西。对于新手来说,一切都很好,但我想知道为什么最快,为什么不说最简单或最简单。
  • 具有讽刺意味的是,列出所有可能的组合可能是对五个数字进行排序的最快方法,而最快确实意味着比任何其他方法花费的 CPU 时间更少。

标签: c++ max min


【解决方案1】:

标准库有一个名为std::sort 的函数,它适用于任何范围。您可以将数字放入数组中,也可以将它们放入其他包含(如std::vector)中。在这种情况下,考虑到五项限制,数组就足够简单了。例如:

#include <iostream>
#include <algorithm>
#include <array>
#include <iterator>

int main() {
    std::array<int, 5> numbers;

    std::cout << "Enter five integers. \n\n>";

    // Read numbers into array.
    for (auto & i : numbers) {
        std::cin >> i;
    }

    // Sort the entire array.
    std::sort(std::begin(numbers), std::end(numbers));

    // Display sorted numbers.
    for (auto i : numbers) {
        std::cout << i << '\n';
    }

    return 0;
}

【讨论】:

    【解决方案2】:

    这里的大错误是将值读入单个变量。 C++ 有许多不同的容器可以保存多个值:数组、std::vectorstd::array。但是有一个有一个神奇的属性,它始终保持所有项目的排序:std::multiset

    cout << "Enter five integers. \n\n>";
    std::multiset<int> a;
    for (int i = 0; i < 5; ++i)
    {
        int v;
        cin >> v;
        a.insert(v);
    }
    for (auto it = a.begin(); it != a.end(); ++it)
        cout << *it;
    cout << "\n";
    

    【讨论】:

      【解决方案3】:

      您可以使用std::vector 存储输入,然后使用std::sort 对元素进行排序。

      #include <iostream>
      #include <algorithm>
      #include <vector>
      
      using namespace std;
      
      int main(){
          vector <int> nums(5);
          
          for(int i = 0; i < 5; ++i){
              cin >> nums[i];
          }
      
          sort(nums.begin(), nums.end());
      }
      

      【讨论】:

        【解决方案4】:

        替代std::sort,您可以使用std::multiset 对元素进行排序:

        #include <iostream>
        #include <fstream>
        #include <cmath>
        #include <algorithm>
        #include <vector>
        #include <set>
        
        using namespace std;
        
        int main(){
            multiset <int> nums;
            int input;
            
            for(int i = 0; i < 5; ++i){
                cin >> input;
                nums.insert(input);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2013-12-04
          • 1970-01-01
          • 2013-03-27
          • 2011-06-28
          • 2021-05-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多