因为水王是每一贴必回,那么他的总数站在所有总数的一半以上.我们就假设第一个是水王,让他跟后一个比较,相同就报留,不同就减去。
import java.util.*; public class water { 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; System.out.println("人们 ID:"); for(i=0;i<count;i++) { a[i]=sca.nextInt(); } sw=a[0]; for(i=1;i<count;i++) { if(sw!=a[i]) { j=j-1; if(j<=0) { sw=a[i+1]; j=1; i++; } } else { sw=a[i]; j=j+1; } } System.out.println("水王是: "+sw); } }