Example of finding water King in class practice

Source: Internet
Author: User

• Title: Three people have designed a irrigation forum. 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 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 IDs. According to the statistics of their posts more than 1/4, you can quickly find them from the list of posts? Idea: According to the teacher class tips "consumer music" method, because the number of water King post more than half, so two different posts to eliminate, the last remaining is definitely the water King ID reference code:
#include <iostream.h> #include "stdlib.h" int main () {int length;    int idnum[3]={0,0,0};          int id[3]={0,0,0};    cout<< "Please enter the total number of posts:" <<endl;    cin>>length;     int * Curid=new int [length];    cout<< "Please enter the ID list of the Navy" <<endl;    for (int j=0;j<length;j++) {cin>>curid[j];        } for (int i=0;i<length;i++) {if (idnum[0]==0 && curid[i]!=id[1] && curid[i]!=id[2])            {idnum[0]=1;        Id[0]=curid[i];            } else if (idnum[1]==0 && curid[i]!=id[0] && curid[i]!=id[2]) {idnum[1]=1;        Id[1]=curid[i];            } else if (idnum[2]==0 && curid[i]!=id[0] && curid[i]!=id[1]) {idnum[2]=1;        Id[2]=curid[i];            } else if (Curid[i]!=id[0] && curid[i]!=id[1] && curid[i]!=id[2]) {idnum[0]--;            idnum[1]--;        idnum[2]--; } else IF (Curid[i]==id[0]) {idnum[0]++;        } else if (Curid[i]==id[1]) {idnum[1]++;        } else if (Curid[i]==id[2]) {idnum[2]++; }} cout<< "three bucket ID:" <<ID[0]<< "" <<ID[1]<< "<<ID[2]<<" "<<end l;+ return 0;}

Sentiment: The code itself is not complex, but the important is the method, how to save time and effort to solve the problem is the core!

Example of finding water King in class practice

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.