Title: Three people to design 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? Ideas: The following information can be obtained according to the topic: 1. Water King will reply to each ID of the post 2. Water King posts more than half the number of forum posts according to these two points, I think you can find each ID of the reply ID number, to find the water king (because the water King will reply to the other ID of each post, so by querying each ID ID, there must be a number of duplicate IDs, that is, water king), but this method of query is more cumbersome. Through other people's explanation, get another way of thinking: the principle of anti-music game to define the initialization of variables N and M, with M to represent the ID, whenever m and the current ID is not the same, n minus 1, and vice versa, plus 1; when n is less than 0 o'clock update m for the current ID. The code is as follows:
#include <iostream> #include <string>using namespace std; #define N 5void Main () { int n = 0; String A[n], m; cout << "Input ID:"; for (int i = 0; i < N; i++) { cin >> a[i]; } m = a[0]; for (int i = 0; i < n; i++) { if (m = = A[i]) { N + = 1; } else N-= 1; if (n < 0) { m = a[i]; } } cout << "Water King is:" << m << Endl;}
Find the Water king.