Water King ID

Source: Internet
Author: User

Topic:

A three-person line designed 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?  First, the design of the idea 1, it is sorted, because the water king appears more than half of the number of posts, so in the middle of the sorting point must be water king, but the complexity of time is too high. 2, eliminate the different two, because the water king post more than half of the total number of posts, so the last remaining must be the water king. The complexity is n two, the source code
#include <iostream.h>int main () {    int n,m=1,water;    int a[10]={3,4,2,2,5,5,6,2,2,2};    WATER=A[0];      for (n=1;n<10;n++) {           if (Water!=a[n])     {                m=m-1;               if (m<=0)      {                         water=a[n+1];             M=1;                        n++;          }               }            else        {             water=a[n];       m=m+1;              }             }        cout<< "Waterman ' s ID is:" <<water<<endl;      return 0;}

Third, the experiment

Iv. Summary of the experiment

After finding a solution, consider the problem of time complexity before you think about the optimal solution.

Code optimization and importance, so we must think deeply, a program is perfect, time complexity is accounted for a large proportion.

Water King ID

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.