编程之美-寻找发帖水王

Type Find(Type *ID,int N)
{//寻找发帖"水王",假设只有一个
	Type candidate;
	int nTimes,i;

	for( i = nTimes = 0; i < N; i++)
	{
		if(nTimes == 0)
		{
			candidate = ID[i];
			nTimes = 1;
		}
		else
		{
			if(candidate == ID[i])
				nTimes++;
			else
				nTimes--;
		}
	}
	return candidate;
}


void Findall(Type *ID,Type *id1,Type *id2,Type *id3,int N)
{//寻找发帖"水王",假设有多个
	Type candidate1,candidate2,candidate3;
	int nTime1,nTime2,nTime3,i;
	Type noexitID = 127;

	nTime1 = nTime2 = nTime3 =0;
	candidate1 = candidate2 = candidate3 = noexitID;

	for(i = 0 ; i < N; ++i)
	{
		if(candidate1 == ID[i])
		{
			nTime1++;
		}
		else if(candidate2 == ID[i])
		{
			nTime2++;
		}
		else if(candidate3 == ID[i])
		{
			nTime3++;
		}
		else if(nTime1 == 0 )
		{
			candidate1 = ID[i];
			nTime1 = 1;
		}
		else if(nTime2 == 0 )
		{
			candidate2 = ID[i];
			nTime2 = 1;
		}
		else if(nTime3 == 0)
		{
			candidate3 = ID[i];
			nTime3 = 1;
		}
		else 
		{
			nTime1--;
			nTime2--;
			nTime3--;
		}
	}

	*id1 = candidate1;
	*id2 = candidate2;
	*id3 = candidate3;
}

    原文作者:hello2sys_cs
    原文地址: https://blog.csdn.net/hello2sys_cs/article/details/8667937
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞