寻找发帖水王02

  题目:随着论坛的发展,管理员发现水王没有了,但是统计结果表明,有3个发帖很多的ID。据统计他们的发帖数目都超过了帖子总数目的1/4,你能从发帖列表中快速找出他们吗?

  设计思路:

  水王01只需要一个结果,而现在需要3个结果,上题用到的nTimes,也应改为3个计数器。现在需要3个变量来记录当前遍历过的3个不同的ID,而nTimes的3个元素分别对应当前遍历过的3个ID出现的个数。如果遍历中有某个ID不同于这3个当前ID,就判断当前3个ID是否有某个的nTimes为0,如果有,那这个新遍历的ID就取而代之,并赋1为它的遍历数(即nTimes减1),如果当前3个ID的nTimes皆不为0,则3个ID的nTimes皆减去1。

  实现代码:

  

//寻找发帖水王02
//胡浩特 2016年5月26日

#include <iostream>
using namespace std;

int candidate[3];
int ntime[3] = { 0 };

int input[100];
int num = 0;

int main()
{
	cout << "please input" << endl;
	int t;
	while (cin >> t)
	{
		if (t == -1)
			break;
		input[num++] = t;
	}

	bool flag = false;

	for (int i = 0; i < num; i++)
	{
		flag = false;
		for (int j = 0; j < 3; j++)
		{
			if (ntime[j] == 0)
			{
				continue;
			}
			if (candidate[j] == input[i])
			{
				ntime[j]++;
				flag = true;
			}
		}

		if (flag == true)
		{
			continue;
		}

		for (int j = 0; j < 3; j++)
		{
			if (ntime[j] == 0)
			{
				candidate[j] = input[i];
				ntime[j]++;
				flag = true;
				break;
			}
		}

		if (flag == true)
		{
			continue;
		}

		for (int j = 0; j < 3; j++)
		{
			ntime[j]--;
		}

	}

	cout << ntime[0] << " " << ntime[1] << " " << ntime[2] << endl;
	cout << candidate[0] << " " << candidate[1] << " " << candidate[2] << endl;
}

  

原文地址:https://www.cnblogs.com/JYQ-hu/p/5532392.html