First, the topic 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. 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 design ideas of how many posts in the forum, how many IDs are not clear. We do this in a way that is similar to consumer music, by traversing all posts and comparing the IDs of two posts. If the ID of two posts is the same, then two posts are discarded. The next comparison, until the remaining posts in the ID only one, the ID output, that is to find the "Water king." Third, the program source code
#include <iostream>using namespace Std;int curid=0;//Current irrigation Idint idnum=0;//Current number of irrigation IDs void deal (int arr[],int N) {for (int i=0;i<n;i++) { if (idnum==0)//Current irrigation ID number becomes 0 or offset becomes 0{ idnum++;//number of posts +1curid=arr[i];//Curid record current irrigation ID} else { if (curid==arr[i])//If the new irrigation post is the same as the current post ID, the number of IDs +1idnum++; else //If different, offset a current ID idnum--; }}} void Main () {int i;int count=0;//record water King posts int n;//number of posts int * Arr=new int[n];cout<< "Please enter number of posts:"; cin>>n;cout << "Enter each post ID:" <<endl;for (i=0;i<n;i++) {cin>>arr[i];} Deal (Arr,n); for (i=0;i<n;i++) {if (Arr[i]==curid) {count++;}} cout<< "\" Water king \ "ID is:";cout<<curid<<endl;cout<< "Water paste Quantity:"; cout<<count<<endl;}
Four, the operation
Normal processing when the ID is a number
The following error occurs when the ID is a letter or a character
V. Summary of the Experiment
After the experiment is done, there are a lot of follow-up work to be done, algorithm optimization and the ID of the irrigation posts are to solve the problem. Careful analysis will find that the topic said a lot of nonsense, in fact, to convey to us only three points of information, 1, the maximum value of an array; 2, the maximum value is greater than the array of all the number and half; 3. Locate the maximum value and output the position of the array (that is, id). The difficulty of this problem is mainly in the time complexity of the algorithm. The subject still needs to be improved in time complexity and post ID processing above.
April 21 Tuesday Classroom Exercise: Finding the Water king