First, the problem description
• 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? Second, the idea according to Wang Wangfa's post more than half, it can be judged, then the water king in the ID table is sure to have a continuous, the use of "consumer music" method can solve the problem, and continuous same digital retention, different elimination of three, code
1#include <iostream>2 using namespacestd;3 #defineLength 11//set maximum length to4 intJudgeintA[length])5 {6 intFlag;7 intCount//to account for the current number of water Kings .8Flag = a[0];9 for(inti =0; i<length;i++)Ten { One if(flag = = A[i])//If the current water king is the same as the next number A { -Flag =A[i]; -count++; the } - Else //If the water king is not the same as the current number - { -count--;//eliminate a number of water kings and the current number + if(count<1) - { +Flag = a[i+1];//if the number of water kings is less than 1, then the next ID for the water king ACount =1;//Count Reset ati++; - } - } - } - returnFlag; - in } - intMain () to { + intTarget; - intArray[length] = {1,2,3,2,4,2,2,5,5,2,2}; theprintf"ID:"); * for(intI=0; i<length;i++) $ {Panax Notoginsengcout<<array[i]<<"Endl; - } thetarget =judge (Array); +cout<<"Water King ID:"<<target<<Endl; A return 0; the}
Iv. Results of operation
Five, experience
The key is to set the count of 1, flag as the first item in the ID table, and then compare, the next item is equal to the flag, count++, if the water king, so that in the ID
At the end of the table, the value of count must not be 0 (because the number of water king IDs is greater than half the number of ID tables).
Looking for "Water king"