import java.util.Random; public class shuiwang { static Random rand = new Random(); private static int N = rand.nextInt(30)+25; private static int[] ID = new int[N]; //随机ID public static void randID() { int sw = rand.nextInt(50)+10; for(int i=0;i<N;i++) { ID[i] = rand.nextInt(50)+10; System.out.print("ID["+i+"]="+ID[i]+" "); if(ID[i] != sw) { if(i != N-1) { ID[++i] = sw; System.out.print("ID["+i+"]="+ID[i]+" "); } } } System.out.println(); } public static void findsw() { int t=0,m=0; //count[][0]为ID,count[][1]为次数 int[][] count = new int[N][2]; for(int i=0;i<N;i++) { for(int j=0;(j<i)&&(t==0);j++) { if(ID[i] == count[j][0]) { count[j][1]++; t=1; } } if(t==0) { count[m][0] = ID[i]; count[m++][1]++; }else { t=0; } } for(int i=0;i<N;i++) { if(count[t][1] < count[i][1]) { t = i; //System.out.println("t = "+t); } //System.out.print("count["+t+"][1] = "+count[t][1]+", \t"+"count["+t+"][0] = "+count[t][0]+", \t"); //System.out.println("count["+i+"][1] = "+count[i][1]+"count["+i+"][0] = "+count[i][0]+", \t"); } System.out.println("水王:"+count[t][0]); } public static void findsw2() { int c=0,t=0; int[] count = new int[50]; for(int i=0;i<N;i++) { count[(ID[i]-10)]++; if(count[(ID[i]-10)] > c) { c = count[(ID[i]-10)]; t = ID[i]; } if(c > (N/2) ) break; } System.out.println("水王:"+t); } public static String removeCharAt(int pos,String s) { return s.substring(0, pos) + s.substring(pos + 1); } public static void findsw3() { int j=0; String s = null; for(int i=0;i < N;i++) { if(i < N-2) { s += ID[i] + ID[i+1]; if(ID[i] != ID[i+1]) { removeCharAt(j,s); removeCharAt(j,s); i = i+2; } else { removeCharAt(j++,s); } } if(i == N-1) { s += ID[i] + ID[i+1]; if(ID[i] != ID[i+1]) { removeCharAt(j,s); removeCharAt(j,s); } else { removeCharAt(j++,s); } } } System.out.println(s); } public static void main(String [] args) { randID(); findsw(); findsw2(); } }