Find the Water king

Source: Internet
Author: User

A topic requirement:

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, will also reply to other ID issued by each post. There is a rumour that the water Wang FA more than half the number of posts. If you have a list of posts for that forum, and the author ID of the post is in it, can you quickly find the legendary water king?

Two design ideas:

Water King has more than half the number of posts, you can compare each of the two different ID numbers to eliminate. Can take advantage of the idea of the stack, first the first ID number into the stack, then the second ID number into the stack, two to compare, different words will be the second ID number pop-up stack, and then the third ID number is pressed into the stack, in the comparison with the first, the same is left, different pop-up stack, and so on, The last stack left is the water king's ID.

Three program code:

//2016/5/19 20 zoned//Find the Water king#include <iostream>using namespacestd;#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; return 0;}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;    Sqstack S;    Initstack (S); K=k-1;  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;}

Four running results:

Five experience:

In this programming time to use the knowledge of the stack, because the long time has not been a bit forgotten, in the previous data structure in the book on the relevant algorithm to achieve. This is the fact that we should always review the knowledge we have learned.

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.