1. Title:
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.
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?
2. Design ideas:
Using the inverse idea of the elimination of music, the adjacent two different IDs in turn eliminated, because the water king ID more than half of the total, so the final remaining must be the water King ID.
3. Source code:
1#include <iostream.h>2#include <stdlib.h>3 intId=0; 4 intCount=0; 5 voidFindid (intArr[],intN)6 { 7 for(intI=0; i<n;i++) 8 { 9 if(count==0) Ten { Onecount++; AId=Arr[i]; - } - Else the { - if(id==Arr[i]) -count++; - Else +count--; - } + } A } at intMain () - { - intN; - int*arr=New int[]; -cout<<"Please enter the total number of posts:"; -Cin>>N; incout<<"Please enter the ID of each navy:"<<Endl; - for(intI=0; i<n;i++) to { +Cin>>Arr[i]; - } the Findid (arr,n); *cout<<"the water King's ID is:"<<Id<<Endl;; $ return 0;Panax Notoginseng}
4. Results:
5. Experience:
This programming practice is also heavy in the design idea, get the topic or the old idea, first from the most stupid method to start, and then to achieve the optimization of the method, the final time to achieve the complexity of the requirements, this for the future programming development has a great help.
The question of finding water king