水王占数量超过一半
package test; public class test1 { String find(String[] post,int n) { int i,j = 0; String shuiwang = post[0]; for(i=0;i<n;i++) { if(!shuiwang.equals(post[i])) { j=j-1; if(j<=0) { shuiwang=post[i+1]; j=1; i++; } } else { shuiwang=post[i]; j=j+1; } } return shuiwang; } }
package test; import java.util.*; public class test0 { public static void main(String[] args) { // TODO Auto-generated method stub test1 f = new test1(); Scanner sca=new Scanner(System.in); System.out.println("输入帖子数:"); int n=sca.nextInt(); String post[]=new String[n+1]; int i; System.out.println("输入帖子ID:"); for(i=0;i<n+1;i++) { post[i]=sca.nextLine(); } String shuiwang = f.find(post, n); System.out.println("水王ID是 " + shuiwang); } }