Design ideas:
Similar to the previous experiment, but also the use of the idea of elimination, the difference is that this is the first to determine the three ID, they do not phase between the elimination, the last remaining three ID is three of the king;
Code:
Package Text;import Java.util.Scanner; Public classtext_y { Public Static voidMain (string[] args) {Scannerinch=NewScanner (System.inch); intN; System. out. println ("Please enter the total number of posts:"); N=inch. Nextint (); String list[]=NewString[n]; inti; System. out. println ("Please enter ID:"); for(i=0; i<n;i++) {List[i]=inch. Next (); } intj[]=New int[3];//mark the convenience of each water king with Jj[0]=j[1]=j[2]=0; String id[]=Newstring[3];//set a single water king ID for(i=0; i<n-1; i++) { if(j[0]==0)//when the counter is 0 o'clock, re-give the water king ID{j[0]++; id[0]=List[i]; } Else if(j[1]==0) {j[1]++; id[1]=List[i]; } Else if(j[2]==0) {j[2]++; id[2]=List[i]; } Else if(List[i].equals (id[0]))//counter increases when same ID{j[0]++; } Else if(List[i].equals (id[1]) {j[1]++; } Else if(List[i].equals (id[2]) {j[2]++; } Else//22-Phase extinction counter is reduced with different IDs{j[0]--; j[1]--; j[2]--; }} System. out. println ("The three water kings were:"+id[0]+" "+id[1]+" "+id[2]); inch. Close (); }}
Results:
Personal Summary:
Because the design thinking is similar to the last time, so good thinking after the good work.
Classroom Exercises (Water king extension)