package shuiwang; import java.util.Scanner; public class a { public static void main(String[] args) { Scanner sca=new Scanner(System.in); System.out.println("输入帖子总数"); int count=sca.nextInt(); int a[]=new int[count]; int i,j=1,sw;//j为某个ID出现的次数,假设为sw(水王ID)出现的次数 System.out.println("输入帖子ID"); for(i=0;i<count;i++) { a[i]=sca.nextInt(); } sw=a[0];//假设第一个数为水王ID for(i=1;i<count;i++) { if(sw!=a[i]) { j=j-1; if(j<=0) //如果j<=0,则表示sw不是水王ID。因为水王发帖数大于一半,无论怎么抵消,都不会小于等于0 { sw=a[i+1]; //所有要换其他ID为水王的ID j=1; //重新定义水王ID出现次数 i++; } } else { sw=a[i]; j=j+1;//统计水王帖子数 } } System.out.println("水王ID是 "+sw); } }
设计思想
先设输入的第一个id为水王的id,j=1为id出现次数,依次向下遍历,若下一个id是水王的id,则j=j+1,若不是水王的id,则j=j-1,当 j<0时,改变水王id为当前遍历到的id,并重新定义此id的出现次数1,因为水王ID超过半数,所以 j 最后肯定大于零,遍历到最后的id就是水王的ID。
运行截图: