一、题目要求
随着论坛的发展,管理员发现水王没有了,但是统计结果表明,有三个发帖很多的ID。据统计他们的发帖数量超过了1/4,你能从发帖列表中快速找到他们吗?
二、设计思想
通过数每次ID出现的次数来排除一部分,留下最后三个。
三、代码实现
//mao2016.5.28
//找小水王
#include <iostream>
using namespace std;
int candidate[3];
int count[3] = {0};
int input[100];
int num = 0;
int main()
{
cout<<"请输入ID"<<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<<"水王ID为: "<<endl;
cout<<candidate[0]<<" "<<candidate[1]<<" "<<candidate[2]<<endl;
}
四、实现截图
五、个人总结
从找大水王和小水王的代码中可以学习到其实思路一样,就是多了几个。