Find the Water king.

Source: Internet
Author: User
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.

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.