Find the Water King (ii)

Source: Internet
Author: User

First, the topic

A three-person line designed a forum for irrigation. 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 ID. According to the statistics of their posts more than 1/4, you can quickly find them from the list of posts?

Second, design ideas

The idea of this experiment is similar to the previous one, through the "elimination of music" way to solve the problem, but this time the number of water king increased to three, so the number of comparisons will also increase, and to set corresponding variables to record the process of comparison.

Third, the source code

#include <iostream.h>void Data (int L,IntA[]);IntMain () {int l;//Lengthint target[3]={0,0,0};int id[3]={-1,-1,-1}; cout<<"*********************************************************"<<Endl cout<<"Please enter the number of posts:"; Cin>>L cout<<"*********************************************************"<<Endlint * shuiwang=NewInt[L]; Data (L,shuiwang);Forint i=0;i<l;i++) {if (target[0]==0 && shuiwang[i]!=id[1] && shuiwang[i]!=id[2]) {target[0]=1; id[0]=Shuiwang[i]; }Elseif (target[1]==0 && shuiwang[i]!=id[0] && shuiwang[i]!=id[2]) {target[1]=1; id[1]=Shuiwang[i]; }Elseif (target[2]==0 && shuiwang[i]!=id[0] && shuiwang[i]!=id[1]) {target[2]=1; id[2]=Shuiwang[i]; }Elseif (shuiwang[i]!=id[0] && shuiwang[i]!=id[1] && shuiwang[i]!=id[2]) {target[0]--; target[1]--; target[2]--; }Elseif (shuiwang[i]==id[0]) {target[0]++; }Elseif (shuiwang[i]==id[1]) {target[1]++; }Elseif (shuiwang[i]==id[2]) {target[2]++; }} cout<<"*********************************************************"<<Endl cout<<"Water Wang Yi for:"<<id[0]<<Endl cout<<"Water King II is:"<<id[1]<<Endl cout<<"Water Wang San for:"<<id[2]<<endl; Cout<<** endl; return 0; void Data (int l,int< Span style= "color: #000000;" > a[]) {cout<< " "<<ENDL; for (int i=0;i<l;i+ +a[i];}      

Four, the experiment

V. Summary of the Experiment

The experiment compared the previous one with a new step, finding more than one person in the global context. Added difficulty, on the basis of the original, carried out in depth. In fact, the idea is still the same, but there is a corner, in the beginning how can not think out. This experiment classmate to my help very much, I also was on the basis of reference to complete this experiment.

Find the Water King (ii)

Related Article

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.