First, the topic and the design idea
A three-person line designed a forum for irrigation. Information college students like to exchange water on the above, legend in the forum there is a "bucket", he not only likes to post, but also reply to the other ID issued by each post. Rumor has it that three "buckets" posted more than 1/4 of 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 bucket?
(1) The ID of the poster is stored in a one-dimensional array.
(2) using the idea of "eliminating music" games, eliminating elements with different element IDs, the last remaining is "bucket".
(3) The idea of expanding the water king, a change of 3.
(4) Output results.
Second, the source code
//Donglongyan Date: 2015.04.23#include <iostream.h>voidMain () {intarray[ One];//posted by ID intI//count Variable intcount[3];//number ofcount[0]=count[1]=count[2]=0; intst[3];///Bucketscout<<"************** Small program to find buckets ****************"<<Endl; cout<<"Enter the poster ID (11, the ID is replaced by an integer):"<<Endl; for(i=0;i< One; i++) {cin>>Array[i]; } for(i=0;i< One; i++) { if(count[0]==0) {st[0]=Array[i]; count[0]=1; } Else if(count[1]==0) {st[1]=Array[i]; count[1]=1; } Else if(count[2]==0) {st[2]=Array[i]; count[2]=1; } Else if(st[0]==Array[i]) {count[0]++; } Else if(st[1]==Array[i]) {count[1]++; } Else if(st[2]==Array[i]) {count[2]++; } Else{count[0]--; count[1]--; count[2]--; }} cout<<"the ID of three buckets is:"<<st[0]<<" "<<st[1]<<" "<<st[2]<<Endl;}
Iii. Results of operation
Iv. Experience
The topic is very simple, the main is how to optimize, is the time complexity of the reduction. To borrow the water king's code optimization, then the idea of eliminating music is very applicable. The programming implementation is good.
Looking for buckets---software engineering