本文主要是介绍1036: 寻找整数序列的主元素,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
解法:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {int n;cin >> n;vector<int> arr(n);vector<int> tong(1000);for (auto& x : arr) {cin >> x;tong[x]++;}int p=max_element(tong.begin(), tong.end()) - tong.begin();if (tong[p] > n / 2) {cout << p;}else cout << -1;return 0;
}
这篇关于1036: 寻找整数序列的主元素的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!