[PAT解题报告] The Dominant Color
众数问题,从m * n个数中找出出现多于一半的数——保证存在。
本质: 同时删掉两个不同的数,众数不变。 于是我们随便记录一个数x, 来一个数 y, 和x不同的话就把x
,y都扔了,相当于扔掉两个不同的数,和x相同的话,就把计数器加1。
所以操作简化为
(1) x出现次数的计数器加1
(2) x出现次数的计数器减1
#include <cstdio> #include <cstring> #include <string> using namespace std; int main() { int m,n,answer; scanf("%d%d",&m,&n); for (int i = m * n, time = 0; i; --i) { int x; scanf("%d",&x); if (time == 0) { answer = x; } if (x == answer) { ++time; } else { --time; } } printf("%d\n",answer); return 0; }
原题链接: http://www.patest.cn/contests/pat-a-practise/1054