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? First, the design of the idea 1, it is sorted, because the water king appears more than half of the number of posts, so in the middle of the sorting point must be water king, but the complexity of time is too high. 2, eliminate the different two, because the water king post more than half of the total number of posts, so the last remaining must be the water king. The complexity is n two, the source code
#include <iostream.h>int main () { int n,m=1,water; int a[10]={3,4,2,2,5,5,6,2,2,2}; WATER=A[0]; for (n=1;n<10;n++) { if (Water!=a[n]) { m=m-1; if (m<=0) { water=a[n+1]; M=1; n++; } } else { water=a[n]; m=m+1; } } cout<< "Waterman ' s ID is:" <<water<<endl; return 0;}
Third, the experiment
Iv. Summary of the experiment
After finding a solution, consider the problem of time complexity before you think about the optimal solution.
Code optimization and importance, so we must think deeply, a program is perfect, time complexity is accounted for a large proportion.
Water King ID