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?
Design ideas:
Because the water King's blog more than half, so the arbitrary elimination of two different, has been eliminated, the last remaining is the water king.
"Go" source code:
#include <iostream.h>#include<stdlib.h>#defineMAXSIZE 100typedefstruct{ int*Base; int*top; intstacksize;} Sqstack;intInitstack (Sqstack &S) {S.Base=New int[MAXSIZE]; if(! S.Base) exit (OVERFLOW); S.top=s.Base; S.stacksize=MAXSIZE; returnOK;}intMain () {intid[maxsize],k=0, N; cout<<"Please enter all ID numbers: (ends with-1)"<<Endl; for(intj=0;; J + +) {cin>>N; if(n==-1) Break; ID[J]=N; K++; } inte,m; Sqstack S; Initstack (S); for(intI=0; i<k;i++) { if(S.top==s.Base) { *s.top++=Id[i]; } intnum=* (s.top-1); if(num!=id[i+1]&&i<k-1) {e=*--S.top; I++; } Else { *s.top++=id[i+1]; } if(i==k-2) {num=* (s.top-1); cout<<"the water King's ID number is:"<<Endl; cout<<num<<Endl; } } return 0;}
Iv. Results of Operation:
Find the Water king