1. Design Ideas
A) starting point "water King has more than half the number of posts", so water king's name will at least repeat n/2, set a number num to record the number of repetitions of a person's name,num Initialize to 0; define a string Shuiwang to remember the name of the water king.
b) When a name is the same as the latter,num++;num--; when Num to be 0 , the next name is assigned to the Shuiwang .
2. Code implementation
Chen Chang 20143051//2016/5/16 class Practice-Find water King//Wang Wang is half the number of people posting, so the water King Num will never be less than a #include<iostream> #include <string >using namespace Std;void main () {int shuinum;cout<< "Please enter the number of posts:" cin>>shuinum;//dynamically create an array of the number of water stickers, The person who is used to store each post is string *shui = new string[shuinum];//string shui;cout<< "Please enter the name of the person who posted each post:" <<endl;for (int i=0;i <shuinum;i++) {cout<<i+1<< ":"; cin>>shui[i];} int num=0; Number of repetitions of string shuiwang=shui[0]; Assume that the water king is the first person for (int i=0;i<shuinum-1;i++) {if (shui[i]==shui[i+1]) num++;else{if (num==0) //When num=0, substitutions Shuiwang =shui[i+1];else num--;}} cout<< "Water King is:" <<shuiwang<<endl;}
3. Results
1.
2.
3.
4. Personal Summary
This class practice has not been solved in the classroom, even in the classroom there is no practical solution. The final design ideas are still found on the internet, people think really good.
As far as this problem is concerned, the point is that "water king has more than half the number of posts", and half of the total number of n/2,
More than half is >n/2, know where the focus, but will not be specific to the problem-solving ideas, their "water" is not deep enough. To learn some good design ideas, more internet search information
Find the Water king