First, the topic requirements
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, design ideas
(1), I think of the first is the ID of the post statistics, and then in the comparison, the biggest will be the water King of the post, but my method of space complexity is high, then changed a method.
(2), I see the post more than normal, that will be the ID of the post sort, that the middle of the water King must be the ID, through this method of time complexity O (n*n).
(3), there is a more simple way of thinking, is to eliminate music thought: Since the water King's post for more than half, then the next post to delete the remaining must be the water King's post, this method optimization of the highest.
Third, the program code
1#include <iostream.h>2 intMain ()3 {4 intnum[ +];5 intI,n;6 7 intCount=1;8cout<<"Enter the number of IDs:";9Cin>>N;Tencout<<"enter ID:"; One for(i=0; i<n;i++) A { -Cin>>Num[i]; - //cout<<num[i]; the - } -cout<<Endl; - inta=num[0]; + for(i=0; i<n;i++) - { + if(num[i]==a) A { atcount++; - - } - Else - { -count--; in } - if(cout==0) to { +A=Num[i]; -Count=1; the } * $ }Panax Notoginsengcout<<"Water King's ID:"<<a<<Endl; - return 0; the}
Iv. Results
V. Summary
When dealing with an event, we will deal with him first and foremost, but the easiest way to think about it is not necessarily the most appropriate approach, we have to figure out a way to analyze the problem, try to find a more suitable way, so that we can progress, to have a breakthrough, Instead of solving problems just to solve them.
Find Water King