一,题目说明
随着论坛的发展,管理员发现水王没有了,但是统计结果表明,有三个发帖很多的ID。据统计他们的发帖数量超过了1/4,设计程序从发帖列表中快速找到他们。
二,设计思路
由于发帖数超过四分之一,所以每次消除四个不同的id,用count来记录判断,最后剩下的就是那三个人。
三,程序代码
#include <iostream>
using namespace std;
int candidate[3];
int count[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 (count[j] == 0)
{
continue;
}
if (candidate[j] == input[i])
{
count[j]++;
flag = true;
}
}
if (flag == true)
{
continue;
}
for (int j = 0;j < 3;j++)
{
if (count[j] == 0)
{
candidate[j] = input[i];
count[j]++;
flag = true;
break;
}
}
if (flag == true)
{
continue;
}
for (int j = 0;j < 3;j++)
{
count[j]--;
}
}
cout<<count[0]<<" "<<count[1]<<" "<<count[2]<<endl;
cout<<candidate[0]<<" "<<candidate[1]<<" "<<candidate[2]<<endl;
}
程序截图:

原文:http://www.cnblogs.com/a1264393659/p/6961938.html