实验题目:

      三人行设计了一个灌水论坛。信息学院的学生都喜欢在上面交流灌水,传说在论坛上有一个“水王”,他不但喜欢发帖,还会回复其他ID发的每个帖子。坊间风闻该“水王”发帖数目超过了帖子数目的一半。
如果你有一张当前论坛的帖子(包括回帖)列表,其中帖子的作者的ID也在其中,你能快速的找到这个传说中的水王吗?
随着论坛的发展,水王没有了,但是有三个发帖很多的id,发帖数量超过四分之一,你能快速找到他吗?

设计思想:

      由于发帖数超过四分之一,所以每次消除四个不同的id,用count来记录判断,最后剩下的就是那三个人。

源代码:

 1 import java.util.Scanner;
 2 public class TheKingOfWater2 {
 3 
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6         Water2 w = new Water2();
 7         w.setLength();
 8         w.findWater2();
 9     }
10 
11 }
12 
13 class Water2
14 {
15     String id[];
16     int length;
17     int i,j;
18     String three[];
19     int count[];
20     void setLength()//输入总帖数
21     {
22         System.out.println("请输入总帖数");
23         Scanner l = new Scanner(System.in);
24         length = l.nextInt();
25     }
26     void findWater2()//查找发帖的三个人
27     {
28         id = new String [length];
29         three = new String [3];
30         count = new int [3];
31         
32         System.out.println("请输入id");
33         for(i =0;i<length;i++)//输入ID
34         {
35             Scanner s = new Scanner(System.in);
36             id[i] = s.nextLine();
37         }
38         
39         for(i =0;i<3;i++)
40         {
41             count[i] = 0;
42         }
43 
44         for(i= 0;i<length;i++)
45         {
46             int m=0;
47             for(j=0;j<3;j++)
48             {
49                 if(count[j]==0)
50                 {
51                     continue;
52                 }
53                 if(three[j] == id[i])
54                 {
55                     count[j]++;
56                     m = 1;
57                 }
58             }
59             
60             if(m == 1)
61             {
62                 continue;
63             }
64             for( j=0;j<3;j++)
65             {
66                 if(count[j] == 0)
67                 {
68                     three[j] = id[i];
69                     count[j]++;
70                     m = 1;
71                     break;
72                 }
73             }
74             
75             
76             if(m == 1)
77             {
78                 continue;
79             }
80             for( j=0;j<3;j++)
81             {
82                 count[j]--;
83             }
84 
85         }
86         System.out.println("三个水王是:");
87         for(j=0;j<3;j++)
88         {
89             System.out.println(three[j]);
90         }
91     }
92     
93 }

截图: