First, the topic
A three-person line designed a forum for irrigation. Information college students like to exchange irrigation above, legend in the forum there is a "water king", he not only likes to post, but also reply to other ID issued by each post. 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?
Second, the design idea:
Because the "water king" posts more than half the number of posts, can be found by the author ID of the next two posts is the same. If the next two IDs are different, then the two IDs will be skipped, if they are equal, if there is no doubt that the "Water king" ID, if not, then the current ID can be considered as the current "Water King", if there is already suspected water King ID, then determine whether the current ID and suspected water King ID is the same, if different, Then reduce the current number of times the ID of the water king successive occurrences of the record, if the same, increase the number of times the water king ID consecutive occurrences. Continue to the following comparison and know the end. If the number of posts is singular, consider the last ID. Through the above thought, we can know that the time complexity is O (n).
Third, the code implementation
#include <iostream>using namespacestd;intMain () {inta[10000],length=-1I//a[10000] The author Id,length used to store each post to record the number of posts intb[2]; //B[0] is used to record the current thought of "Water king" ID//b[1] is used to record the number of times the "Water king" is currently perceived as being more contiguous than other users while(length<1) {cout<<"Please enter the number of posts:"<<Endl; CIN>>length; } cout<<"Please enter the author ID of each post in turn:"<<Endl; for(i=0; i<length;i++) {cin>>A[i]; } b[0]=a[0]; b[1]=0; for(i=0; i<length-1; i++) { if(a[i]==a[i+1]) { if(b[1]>0)//currently found "Water king" { if(a[i]==b[0]) {b[1]++; } Else{b[1]--; } } Else//There is no current search for the water king{b[0]=A[i]; b[1]++; } I++; } Else if(a[i]!=a[i+1]) {i++; } if(i==length-2)//For the number of posts that appear but singular, the last number is processed { if(b[1]==0) {b[0]=a[i+1]; } }} cout<<"the "Water King" ID is:"<<b[0]<<Endl; return 0;}
Iv. Realization OF
Example 1: Assume that there is only one ID
Example 2: Suppose the number of posts is even
Example 3: Suppose the number of posts is odd
Example 4: Assume that only the beginning has a sequential ID
Example 5: Assume that only successive IDs appear at the end
V. Personal SUMMARY
This experiment, when I first saw the subject, I didn't quite understand its meaning. I have my own thoughts when the teacher says, "Compare adjacent two IDs are the same, keep the same, and then delete". In the beginning, I think is the different adjacent two number of deleted, the same is retained one, and then to compare, but so the complexity of time is larger, there are errors. Finally, I focus on the water King post more than half of the number of posts, the water King ID consecutive occurrences of more than equal to the number of consecutive occurrences of the sum of other users, through this I think of the current suspected water King ID consecutive occurrences of the number of times compared to other users of the current number of consecutive times to find the water king. Through this experiment, I learned to control the complexity of time to write programs, in the program to solve the problem, we must read the core of the problem, to find a breach of the problem, so as to solve the problem.
Software Engineering class assignment--looking for "water king"