Class practice-Finding 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?

Problem-solving ideas: According to the discussion in class, the design should be the time complexity O (n). The elimination method to solve the problem: The next two ID if the difference is eliminated, stay in the final must be water King ID (title "More than half of the number of posts" is very important). The ID account given in the program is assumed to be 10.

Program code:

#include <iostream.h>intMain () {inta[Ten]; cout<<"Please enter 10 ID accounts:"<<Endl;  for(intk=0;k<Ten; k++) {cin>>A[k]; }    inttemp=a[0]; intj=1;  for(intI=1;i<Ten; i++)    {            if(a[i]==a[0]) J++; ElseJ--; if(j==0) {Temp=a[i+1]; J=1; }} cout<<"the Water king's account ID is:"<<temp<<Endl; return 0;}

Program:

Thinking Summary:

In the design program, the first thought is to determine whether the two adjacent IDs are the same, as the first condition to program. But did not achieve the effect of ID elimination, the result is more chaotic. After breaking the thinking, consider the use of counting methods. Base 1, the first ID as the basis, the same cumulative, different exhaustion, when it is 0 o'clock, the number of the elimination. In the failure of the program, and re-deepened the basic language of C programming ability, the program is successful, the code is not so difficult to imagine. Just didn't think of the proper method.

Class practice-Finding 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.