First, design ideas
Title 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.
id in which, can you quickly find the legendary water king? reference 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 ID. According to statistics they have more than the number of posts , you can quickly find them from the list of posts?
Design ideas:
A similar idea to looking for a water king:
1, counter and suspect variables are set to an array.
2, each time to select an ID and three suspects compared, if there is the same counter plus 1, if three are not the same, then three counters are reduced by one. If it is reduced to 0, the suspect is re-assigned.
Second, the program source code
1 //bucket. Cpp:defines The entry point for the console application.2 //3 4#include"stdafx.h"5#include"iostream.h"6 #defineMAXSIZE 307 8 9 voidFind (intId[],intLenintwater[])Ten { One intcounter[3];//counter Acounter[0]=counter[1]=counter[2]=0; -water[0]=water[1]=water[2]=-1;//Initialize list of suspects - for(intI=0; i<len;i++) the { - if(counter[0]==0)//Assigning values First - { -counter[0]++; +water[0]=Id[i]; - } + Else if(counter[1]==0) A { atcounter[1]++; -water[1]=Id[i]; - } - Else if(counter[2]==0) - { -counter[2]++; inwater[2]=Id[i]; - } to Else if(id[i]==water[0])//If counter is 0, re-assign the value + { -counter[0]++; the } * Else if(id[i]==water[1]) $ {Panax Notoginsengcounter[1]++; - } the Else if(id[i]==water[2]) + { Acounter[2]++; the } + Else //if the IDs of the three suspects are different, the three counters are reduced by one until the counter is 0 . - { $counter[0]--; $counter[1]--; -counter[2]--; - } the } - }Wuyi the intMainintargcChar*argv[]) - { Wu intId[maxsize];//Post record form - intnumber,waterid[3];//number of posts, buckets Aboutcout<<"Please enter the number of posts:"; $Cin>>Number ; -cout<<"Please enter the Post ID:"; - for(intI=0; i<number;i++) - { ACin>>Id[i]; + } the Find (Id,number,waterid); -cout<<"buckets are:"; $ for(i=0;i<3; i++) the { thecout<<waterid[i]<<" "; the } thecout<<Endl; - return 0; in}
Iii. Results of operation
Iv. Summary and experience
This experiment refers to the online design ideas and code, I really did not think of this method, through reference, let my ideas more open, but also richer, which is also a way to learn, and hope to learn more.
Classroom exercises--Find the Water king continued