Topic:
• Three people have designed a irrigation forum.
students at the school of information are fond of exchanging water on top,
legend has a "water king" on the forum, he not only likes to post,
it will also reply to each post sent by other IDs. 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? 1. Design idea: Delete Two different IDs each time, in the remaining ID, the original maximum frequency ID appears as much as more than 50%, repeat this process, the last remaining will be all the same ID, that is, water king.
2. The code is as follows:
#include<iostream>
using
namespace
std;
int
FindID(
int
num[],
int
n)
{
int
index = 0,count = 0;
for
(int i = 0; i < n; i++)
{
if
(count == 0)
{
index = num[i];
count = 1;
}
else
{
if
(index == num[i])
{
count++;
}
else
{
count--;
}
}
}
return
index;
}
int
main()
{
int
num, shui;
int
arr[] = {2,2,3,3,5,2,8,2,2,12,2,2,3,9,2,2,2,7,4,2};
num =
sizeof
(arr)/
sizeof
(
int
);
shui = FindID(arr, num);
cout<<
"水王是"
<<shui<<endl;
}
3. Results:
4. Personal Summary:
There is no good abstract problem, in fact, the original problem can be abstracted to give you an array, there are more than half of the number is the same, find out the number of the most occurrences, the algorithm is not very good design, the first thing is to sort and then output the median, and finally in the inspiration of others to complete.
Software engineering work-Finding water king