PAT 1054. The Dominant Color (20)题解

求出现次数超过一半的元素,采用moore's voting算法.

#include 
/*
Created by HarvestWu on 2018/4/25.
*/
int main()
{
	int m ,n;
	scanf("%d %d", &m, &n);
	int count = 0, t = -1, a;
	for (int i = 0; i < m*n; i++){
	    scanf("%d", &a);
	    if (count == 0){ t = a; count++; }
	    else{
		if (t == a) count++;
		else count--;
	    }
	}
	printf("%d", t);
	return 0;
}

你可能感兴趣的:(PAT,Practice)