Find the Water king

Source: Internet
Author: User
Tags net time

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?

Design ideas: According to the topic, we can know the number of posts and ID, and the most critical one is that the number of water king posts more than half of the total number of posts, so we can set a variable count to count, first assume that the water king is the first ID of the person, starting from the first post, if The ID of the second post is the same as the ID of the first post, Count plus 1, if different, count minus 1, and finally if count is less than 0, then the water King becomes the next ID, if greater than 0, is the first ID. You can find the king of water by walking through it.

Source:

1 //2016/5/9, Zhangxiaofi, the question of finding water king2#include <iostream>3 using namespacestd;4 5 #defineN 1006 7 intMain ()8 {9     intnum;Tencout<<"Please enter the number of posts:"; OneCin>>num; A      while(num<=0) -     { -cout<<"Please re-enter the number of posts:"; theCin>>num; -     } -  -     intId[n]; +cout<<"Please enter ID:"; -      for(intI=0; i<num;i++) +     { ACin>>Id[i]; at     } -  -     intshuiwang=id[0];//define Shuiwang this variable -     intCount//used to Count -      for(intj=0; j<num;j++)//iterate from the first ID, if the first and second post IDs are the same, Count plus 1, minus 1 for different -     { in         if(shuiwang=Id[j]) -         { tocount=count+1; +         } -         Else thecount=count-1; *      if(count<0)//if count is less than 0, the water king equals the next ID, and then loops through the $         {Panax NotoginsengShuiwang =Id[j]; -         } the      } +  Acout<<"The Water king is:"<<shuiwang<<Endl; the     return 0; +}

Results:

Summary: Just got this topic when a little thought, then listen to the class of the great God to talk about ideas, only to know that the original can write, know the idea of writing on the faster, in fact, a topic the most important is the idea, no idea what can not be done, so, exercise a good thinking is very important.

Bug log:

The drawback is that if the user accidentally entered the number of letters, my program will not be able to handle errors.

Time Logging:

14:00
date start time end time Interrupt Time (min) net time (min) Activities remarks

May 16

Monday

15:50 10 (recess) + class Software engineering

May 17

Tuesday

20:00 22:00 0 programming Big jobs

 5 month 18th

20:00 22:00 0 + programming Find water king

May 19

Thursday

15:50 ten 2 on machine software engineering

May No. 0 #

Friday

16:30 17:30 0 Organize your blog, post a blog

&NB SP;

Find water King

  

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.