找水王(续)

一、题目:

  1 三人行设计了一个灌水论坛。信息学院的学生都喜欢在上面交流灌水,传说在论坛上有一个“水王”,他不但喜欢发帖,还会回复其他ID发的每个帖子。坊间风闻该“水王”发帖数  目超过了帖子数目的一半。
  2 如果你有一张当前论坛的帖子(包括回帖)列表,其中帖子的作者的ID也在其中,你能快速的找到这个传说中的水王吗?(参考核心代码)
  3 随着论坛的发展,管理员发现水王没有了,但是统计结果表明,有三个发帖很多的ID。据统计他们的发帖数量超过了1/4,你能从发帖列表中快速找到他们吗?
二、设计思想
  给三个水王命名为result1,result2,restult3,首先先给result1,result2,restult3赋值,即赋值给不同的三个ID,如果后面的ID和result不一样,则times都减1,即去掉三个不相同的数据,最后得到的result1,result2,restult3即为水王。
三、源代码
 1 // 信1201-2班 司新红 
 2 
 3 #include "stdafx.h"
 4 #include "iostream"
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     int a[16]={1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,3};
10     int result1;
11     int result2;
12     int result3;
13     int times1=0;
14     int times2=0;
15     int times3=0;
16     for(int i = 0; i < 16; i++)
17     {
18         if(times1==0)
19         {
20             result1=a[i];
21             times1=1;
22         }
23         else
24         {
25             if(a[i]==result1)
26             {
27                 times1++;
28             }
29             else
30             {
31                 if(times2==0)
32                 {
33                     result2=a[i];
34                     times2=1;
35                 }
36                 else
37                 {
38                     if(a[i]==result2)
39                     {
40                         times2++;
41                     }
42                     else
43                     {
44                         if(times3==0)
45                         {
46                             result3=a[i];
47                             times3=1;
48                         }
49                         else
50                         {
51                             if(a[i]==result3)
52                             {
53                                 times3++;
54                             }
55                             else
56                             {
57                                 times1--;
58                                 times2--;
59                                 times3--;
60                             }
61                         }
62                     }
63                 }
64             }
65         }
66     }
67     cout<<"水王为:"<<endl<<result1<<endl<<result2<<endl<<result3<<endl;
68     return 0;
69 }

四、运行结果

五、个人总结

  我感觉这次的程序有点困难,刚开始看到这个题的时候一单思路都没有,还有就是在编写程序的时候,出现了一个小小的问题,就是在判断语句的时候应该用"==",结果我用的“=”,导致程序结果一直运行不正确。

 
posted @ 2015-04-28 18:28  幽梦  阅读(234)  评论(0编辑  收藏  举报