Topic:
• Three people have designed a irrigation forum. students at the school of information are fond of exchanging water on top,legend has a "water king" on the forum, he not only likes to post,it will also reply to each post sent by other IDs. The "Water King" has been rumored to have posted more than half the number of posts. • If you have a list of posts (including replies) for the current forum, and the ID of the author of the Post is in it, can you quickly find the legendary water king? Design ideas:1. You can simply do an abstraction of the topic: give you an array with more than half of the numbers in it, and your task is to find the number. 2. The first thought is that the number in the array to sort, and then traverse, the most important nickname is "Water King", but it is obviously not a simple way, pass. 3. Once again, you can randomly extract dozens of data in a large array, and then search for the most ID is "Water king", but this program is not rigorous, the complexity of time has not changed, pass. 4. The time complexity of both of these ideas is O (n * log2n + N). 5. Now, it can be thought that if two different numbers are deleted each time (regardless of including the highest frequency), then, in the remaining number, the original highest frequency appears as much as 50%, repeating the process, the last remaining will be all the same number, that is, the highest frequency number. This algorithm avoids sorting, and the time complexity is only O (N).
The code is as follows:
public class Find { int[] id=new int[100]; int Rand ()//generates a random number within 0-1 { java.util.Random random=new java.util.Random ();//defines a random class int result= Random.nextint (2);//Returns the integer in the [0,2] collection, noting that the return result is not included ; void Firstc ()//initialize array; {for (int i=0;i<100;i++) {if (Rand ()!=0) {id[i]=0;//water King's ID}else{id[i]=i;}}} void out () {System.out.println ("Below is the ID of the Water sticker"); for (int i=1;i<=id.length;i++) {System.out.print (id[i-1]+ ""); if (i%5= =0) {System.out.print ("\ n");}}} int Serch ()//thought implementation part {int candidate=50; int ntimes,i; for (i = Ntimes = 0;i<id.length;i++) { if (Ntimes = = 0) { candidate = id[i]; Ntimes = 1; } else{ if (candidate = = Id[i]) ntimes + +; else ntimes--; } } return candidate;} public static void Main (string[] args) {Find a = new Find (); A.FIRSTC (); a.out (); System.out.println ("Water King's ID is" +a.serch ());}}
Implementation: Modify the code to change the water King's ID to other digital verification correctness::Personal Summary:There are many ways to implement a program, but a good engineer can always find a simpler algorithm to implement it in a short period of time, which is irrelevant to the ability of programming, and the key is whether there are ideas or ideas. a relatively simple implementation method for the computer may save a lot of time and space, but also is the root of the program robustness, the idea of the program writer is very important.
Software engineering work--seeking water king