摩尔投票

标题 最简单的算法

#include<stdio.h>
int main(void)
{
    
    
	int a[9] = {
    
    2,2,3,2,2,4,5,2,8};
	int temp = a[0],count = 1;//把a[0]设置成标准,并且它有一票
	int i;
	for(i=1;i<9;i++)
	{
    
    
		if(a[i]==temp)
			count++;
		else
			count--;
		while(count==0)
		{
    
    
			temp = a[i];
			count = 1;
		}
	}
	printf("%d",temp);
}

好了!这就是摩尔投票的简单使用!

猜你喜欢

转载自blog.csdn.net/qq_52208569/article/details/109756496