Find the Water king

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.