• 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