寻找主元素,时间负责度为O(n).
//
// main.cpp
// PATA1054
//
// Created by Phoenix on 2018/2/14.
// Copyright © 2018年 Phoenix. All rights reserved.
//
#include <iostream>
#include <cstdio>
int main(int argc, const char * argv[]) {
int n, m;
scanf("%d %d", &m, &n);
int num = 0, temp = -1, a;
for(int i = 0; i < m * n; i++) {
scanf("%d", &a);
if(num == 0) {
temp = a;
num++;
} else {
if(temp == a) {
num++;
} else {
num--;
}
}
}
printf("%d", temp);
return 0;
}