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? (Refer to the core code) with the development of the Forum, the administrator found that the water king did not, but the statistical results show that there are three posts a lot of ID. According to the statistics of their posts more than 1/4, you can quickly find them from the list of posts?
2 Design ideas:
Similar to the same idea, the use of the idea of eliminating simplification, the elimination of the comparison. Each water King ID can be offset with the other Navy ID. The last remaining is three water king, the first three different Navy ID to the Water King ID, start to offset the count, "Water king" offset after the amount of 0 is eliminated, with a new Navy ID to continue to cancel, the last remaining is the water King ID.
3 Source program code:
#include <iostream> #include <string>using namespace std; #define N 16void Main () {int id[n]={6,7,8,9,6,7,8,6 , 7,8,6,7,8,6,7,8}; int id_null; int sw[3]; int a[3]; int i; sw[0]=sw[1]=sw[2]=0; A[0]=a[1]=a[2]=id_null; for (i=0;i<10;i++) {if (id[i]==a[0]) {sw[0]++; } else if (Id[i]==a[1]) {sw[1]++; } else if (Id[i]==a[2]) {sw[2]++; } else if (sw[0]==0) {sw[0]=1; A[0]=id[i]; } else if (sw[1]==0) {sw[1]=1; A[1]=id[i]; } else if (sw[2]==0) {sw[2]=1; A[2]=id[i]; } else {sw[0]--; sw[1]--; sw[2]--; }} cout<< "Aqua King is:" <<endl; cout<<a[0]<< "" <<a[1]<< "" <<a[2]<<endl; }
4 results:
Find the Water King continued