1 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?
2 Design ideas:
1 First, if you delete two different IDs at a time, the "Water King" ID will appear more than half of the total in the remaining list of IDs.
2 Repeat this process, reduce the total number of IDs in the ID list, and finally find the water king.
3 Source program code:
#include <iostream>#include<string>using namespacestd;#defineN 20voidMain () {intm =0; stringA[n], N; cout<<"Enter the ID number of the post:" ; for(inti =0; i < N; i++) {cin>>A[i]; }; N= a[0]; for(inti =0; i < N; i++) { if(n = =A[i]) {m+=1; } Elsem-=1; if(M <0) {n=A[i]; }} cout<<"The Water king is:"<<N;}
4 operation Result:
Find the Water King class practice