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, design ideas
(1) The first thing I think about is the ID of the post is counted, and then in the comparison, the biggest will be the water King's post, but my method of spatial complexity, then changed a method.
(2) There is a more simple idea, that is, the idea of eliminating music: Since the water King's post is more than half, then the next post to delete the remaining must be the water king of the Post
#include <iostream>using namespace Std;int main () {int Num[1000];int i, n;int count = 1;cout << "Number of input IDs:"; Cin &G t;> n;cout << "input ID:"; for (i = 0; i<n; i++) {cin >> num[i];//cout<<num[i];} cout << Endl;int a = num[0];for (i = 0; i<n; i++) {if (num[i] = = a) {count++;} else{count--;} if (count = = 0) {a = Num[i];count = 1;}} cout << "Water King's ID:" << a << endl;return 0;}
Program:
Find the Water king