Design ideas:
InputIDTotal, inputID;Suppose the first oneIDthe Water king, if the King of waterIDwith the nextIDDifferent, then eliminate the different, setJequals0minus one;IDThe same, theJadd one; the last remainingIDfor the water king.
Source:
//the water king on the forumImportJava.util.Scanner; Public classtest4 { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); intN; System.out.println ("Please enter the total number of posts:"); N=In.nextint (); String list[]=NewString [N]; intI,j=0; System.out.println ("Please enter ID:"); for(i=0;i<n;i++) {List[i]=In.next (); } String King=list[0]; //A Water king is the first ID for(i=0;i<n-1;i++) { //add the same, eliminate the difference, and the last remaining is the water king if(King.equals (list[i+1])) {J++; } ElseJ--; if(j<0) {King=list[i+1]; }} System.out.println ("Water King is:" +king); In.close (); }}
:
Personal Summary:
The classroom practice is not difficult, mainly we have to clarify the idea, with a sort of calculation and is also possible, but too cumbersome; 22 elimination method can be more simple to calculate the results.
Classroom Exercises Four