如何找到向量的第二个最小值?

时间:2019-09-24 09:35:31

标签: c++

unsigned int i;
int min;
int num;

vector<int>sequence;

cout << "sequence: ";
for (i=0; cin.good(); ++i){
      cin >> num;
      sequence.push_back(num);
}

sequence.pop_back();

min = sequence.at(0);
for (i=0; i<sequence.size();++i){
    if (sequence.at(i)< min){
        min=sequence.at(i);
    }
}

cout << min << endl;

我想找到向量中第二小的用户输入。我已经创建了找到最小代码的代码。但我不知道如何找到第二个最小的。我应该不对数字进行排序。有人可以告诉我一种方法吗?

3 个答案:

答案 0 :(得分:2)

您可以在O(n log n)运行时复杂度中进行操作。排序将花费#include <vector> #include <stdio.h> #include <iostream> using namespace std; int main() { unsigned int i; int min; int num; vector<int>sequence; cout << "sequence: "; for (i=0; cin.good(); ++i){ cin >> num; sequence.push_back(num); } sequence.pop_back(); int minimum = INT_MAX, secondmin = INT_MAX; for (i=0; i<sequence.size();++i){ if (minimum > sequence[i]) { secondmin = minimum; minimum = sequence[i]; }else if ( secondmin > sequence[i] ) { secondmin = sequence[i]; } } cout << "answer is "<< secondmin << endl; } 的时间复杂度。在这种情况下,您只需要两个变量即可找到第二个最小值。

sequence: 1
2
3
4
end
answer is 2

输出

if verb_list[-1] == ["e"]:              #verb_list is a inputed string putted into a list
    verb_list[-1] = "i"
    verb_list.append("n")
    verb_list.append("g")

答案 1 :(得分:0)

一种简单的方法例如可以通过以下方式查看

#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>

int main() 
{
    std::vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    decltype( v )::value_type min1 = 0, min2 = 0;

    if ( not ( v.size() < 2 ) )
    {
        auto minmax = std::minmax( v[0], v[1] );
        min1 = minmax.first;
        min2 = minmax.second;

        for ( decltype( v )::size_type i = 2; i < v.size(); i++ )
        {
            if ( v[i] < min1 )
            {
                min2 = min1;
                min1 = v[i];
            }
            else if ( v[i] < min2 )
            {
                min2 = v[i];
            }
        }
    }


    std::cout << "min1 = " << min1 << ", min2 = " << min2 << '\n';

    return 0;
}

程序输出为

min1 = 0, min2 = 1

或者您可以编写自己的算法,例如

#include <iostream>
#include <utility>
#include <vector>
#include <iterator>
#include <algorithm>
#include <numeric>

template <typename ForwardIterator>
ForwardIterator second_min( ForwardIterator first, ForwardIterator last )
{
    ForwardIterator first_minimum  = first;
    ForwardIterator second_minimum = last;

    if ( first != last )
    {
        while ( ++first != last )
        {
            if ( *first < *first_minimum )
            {
                second_minimum = first_minimum;
                first_minimum  = first;
            }
            else if ( second_minimum == last || *first < *second_minimum )
            {
                second_minimum = first;
            }
        }
    }

    return second_minimum;
}

int main() 
{
    std::vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    auto minimum = second_min( std::begin( v ), std::end( v ) );

    if ( minimum != std::end( v ) )
    {
        std::cout << "second minimum = " << *minimum << '\n';
    }

    return 0;
}

程序输出为

second minimum = 1

答案 2 :(得分:0)

解决方案:

#include <iostream>
#include <algorithm>
#include<vector>

using namespace std;

int main()
{
    unsigned int i;
    int min;
    int num;

    vector<int>sequence;

    cout << "sequence: ";
    for (i = 0; cin.good(); ++i) {
        cin >> num;
        sequence.push_back(num);
    }
    sequence.pop_back();
    sort(sequence.begin(), sequence.end());

    cout << " Second smallest : "<<sequence[1] << endl;
}