三人行设计了一个灌水论坛。信息学院的学生都喜欢在上面交流灌水,传说在论坛上有一个“水王”,他不但喜欢发帖,还会回复其他ID发的每个帖子。坊间风闻该“水王”发帖数目超过了帖子数目的一半。如果你有一张当前论坛的帖子(包括回帖)列表,其中帖子的作者的ID也在其中,你能快速的找到这个传说中的水王吗?
代码:
findWaterKing.java
1 public class findWaterKing { 2 private int id; 3 private String userid; 4 private String type; 5 public bean(int id,String userid,String type) 6 { 7 this.id=id; 8 this.userid=userid; 9 this.type=type; 10 } 11 public int getId() { 12 return id; 13 } 14 public void setId(int id) { 15 this.id = id; 16 } 17 18 public String getType() { 19 return type; 20 } 21 public void setType(String type) { 22 this.type = type; 23 } 24 public String getUserid() { 25 return userid; 26 } 27 public void setUserid(String userid) { 28 this.userid = userid; 29 } 30 31 }
findWaterKingDao.java
1 import java.util.ArrayList; 2 import java.util.List; 3 4 public class findWaterKingDao { 5 public static void main(String[] args) 6 { 7 bean[] num=new bean[10]; 8 num[0]=new bean(1,"001","发帖"); 9 num[1]=new bean(2,"002","回帖"); 10 num[2]=new bean(3,"001","发帖"); 11 num[3]=new bean(4,"003","回帖"); 12 num[4]=new bean(5,"001","发帖"); 13 num[5]=new bean(6,"001","回帖"); 14 num[6]=new bean(7,"005","回帖"); 15 num[7]=new bean(8,"001","发帖"); 16 num[8]=new bean(9,"001","发帖"); 17 num[9]=new bean(10,"006","回帖"); 18 display(num); 19 20 21 } 22 public static void display(bean[] num) 23 { 24 int c = -1; 25 int n=num.length; 26 int m=0; 27 String a=num[0].getUserid(); 28 for(int i=0;i<n;i++) 29 { 30 if(a.equals(num[i].getUserid())) 31 { 32 m++; 33 } 34 else 35 m--; 36 if(m<0) 37 { 38 a=num[i].getUserid(); 39 m=0; 40 } 41 42 } 43 System.out.println("水王是:"+a); 44 } 45 46 }