#include<algorithm> #include<vector> #include<iostream> using namespace std; bool fun(vector<int> v, int& x) { vector<int> v1 = v; sort(v1.begin(), v1.end(), greater<int>()); vector<int>::iterator it1, it2; int size = v1.size(); int count = 0; it2 = v1.begin(); for (it1 = v1.begin(); it1 != v1.end(); it1++) { if (*it1 != *it2) { it2 = it1; count = 1; } else { count++; if (count > size / 2) { x = *it1; return true; } } } return false; } int main() { vector<int> v; int a[] = { 2,9,6,4,5,6,2 }; for (int i = 0; i < 7; i++) { v.push_back(a[i]); } int x = -1; bool res; res = fun(v, x); cout << res << ";" << x << endl; return 0; }
原文:https://www.cnblogs.com/SlowIsFast/p/12718117.html