寻找小水王

一、题目

    三人行设计了一个灌水论坛。随着论坛的发展,管理员发现水王没有了,但是统计结果表明,有三个发帖很多的ID。据统计他们的发帖数量超过了1/4,你能从发帖列表中快速找到他们吗?

二、设计思路

    上一次的思路类似,依然采用两两相消方式解决问题,不过这次水王的数量增加到了三个,所以比较的数量也会有所增加,需要设置相应的变量来记录比较的过程。

三、源代码

 1 #include<iostream.h>
 2 void Data(int l,int A[]); 
 3 int main()
 4 {
 5     int l;//长度
 6     int target[3]={0,0,0};
 7     int ID[3]={-1,-1,-1};      
 8     cout<<"*********************************************************"<<endl;
 9     cout<<"请输入的帖子数量:";
10     cin>>l;
11     cout<<"*********************************************************"<<endl;
12     int * shuiwang=new int [l];
13     Data(l,shuiwang);
14     for(int i=0;i<l;i++)
15     {
16         
17         if(target[0]==0 && shuiwang[i]!=ID[1] && shuiwang[i]!=ID[2])
18         {
19             target[0]=1;
20             ID[0]=shuiwang[i];
21         }
22         else if(target[1]==0 && shuiwang[i]!=ID[0] && shuiwang[i]!=ID[2])
23         {
24             target[1]=1;
25             ID[1]=shuiwang[i];
26         }
27         else if(target[2]==0 && shuiwang[i]!=ID[0] && shuiwang[i]!=ID[1])
28         {
29             target[2]=1;
30             ID[2]=shuiwang[i];
31         }
32         
33         else if(shuiwang[i]!=ID[0] && shuiwang[i]!=ID[1] && shuiwang[i]!=ID[2])
34         {
35             target[0]--;
36             target[1]--;
37             target[2]--;
38         }
39         else if(shuiwang[i]==ID[0])
40         {
41             target[0]++;
42         }
43         else if(shuiwang[i]==ID[1])
44         {
45             target[1]++;
46         }
47         else if(shuiwang[i]==ID[2])
48         {
49             target[2]++;
50         }
51         
52     }
53     cout<<"*********************************************************"<<endl;
54     cout<<"水王一为:"<<ID[0]<<endl;
55     cout<<"水王二为:"<<ID[1]<<endl;
56     cout<<"水王三为:"<<ID[2]<<endl;
57     cout<<"*********************************************************"<<endl;
58     return 0;
59 }
60 void Data(int l,int A[])
61 
62 {
63     
64     cout<<"请输入ID序列:"<<endl;
65     for(int i=0;i<l;i++)
66         
67     {
68         
69         cin>>A[i];
70         
71     }
72     
73 }
原文地址:https://www.cnblogs.com/wulun/p/5590350.html