找到以后要再扫一遍确认。
#include#include #define LEN 100005#define ulong unsigned long longusing namespace std;ulong A[LEN];int main(){ int n; while (cin >> n) { for (int i = 0; i < n; i++) { cin >> A[i]; } int count = 0; ulong candidate = 0; for (int i = 0; i < n; i++) { if (count == 0) { count++; candidate = A[i]; } else if (candidate == A[i]) { count++; } else { count--; } } count = 0; for (int i = 0; i < n; i++) { if (A[i] == candidate) count++; } if (count * 2 > n) { cout << candidate << endl; } else { cout << -1 << endl; } } return 0;}