First, the topic requirements:
• 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 solution of ideas
According to the teacher in class to provide the idea of the elimination of music. Use a different method of elimination, based on the user's unordered ID input. When traversing, compare adjacent two ID, if the inconsistency is eliminated, "Water King" post number more than half of the number of posts, so delete to the last remaining ID even water King's ID, this method effectively reduces the time complexity.
third, the code
#include"stdafx.h"#include<iostream>intFindintArrid[],intnum) { intShuiwang; intx; for(inti=x=0; i<num;i++) { if(x==0) {Shuiwang=arrid[i],x=1; } Else { if(shuiwang==Arrid[i]) { ++x; } Else { --x; }}} printf ("the water King's ID is:%d\n", Shuiwang); returnShuiwang;} intMainintargcChar*argv[]) { intarry[6]={3,4,3,2,3,3}; Find (Arry,6); return 0; }
Four
Five, Experience
This class of training feel the teacher out of the problem more and more Test IQ, when the classmate said the middle ID must be water king's time, I still do not understand what is the matter. Later only to know by the size of the ID sort, water Wang FA paste number is more than half, so the middle of the water king. Then the teacher provides the thought time complexity is small, therefore uses the teacher's method, the programming writing code is not difficult, but the important still is the thought
.
Software Engineering Classroom Training--seeking water king