Find the Water king

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.