找水王

  程序设计思想:由于水王发的帖子占到了全部帖子的一半以上,所以水王的帖子比其他人的帖子和要多,所以我设计的思路就是,从头开始遍历,从第一个人的ID开始,进行比对,如果相同就对数量进行增加,如果不同,就对数量减一,如果为零就重新选择水王,由于水王的帖子最多且大于一半,所以最后剩下的一定就是水王。

  源程序代码:

 1 package 灌水为王;
 2 
 3 public class shuiwang {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7 
 8         int n=9;
 9         int b[]=new int[n+1];
10         int a[]={0,4,3,4,5,4,2,4,3,4,4};
11         int i;
12         int shuiwang=0;
13         int x=0;
14         for(i=0;i<11;i++)
15         {
16             if(x==0)
17             {
18                 shuiwang=a[i];
19                 x=1;
20             }
21             else
22             {
23                 if(shuiwang==a[i])
24                 {
25                     ++x;
26                 }
27                 else
28                 {
29                     --x;
30                 }
31             }
32         }
33                 System.out.println("水王是"+shuiwang);
34             
35         }
36     }

 

  实现截图:

 

  个人总结:整体思路是老师讲的,我只是对思路程序化,并不能说明什么,我还是需要多多努力才能成为一名合格的工程师。

posted @ 2017-04-18 19:45  风飘万点江满月  阅读(126)  评论(0编辑  收藏  举报