The Water king for classroom practice

Source: Internet
Author: User

First, the problem description

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? Second, the design of the idea because by the title, the amount of the network posted more than half of the total number of posts, so will all the ID to find out the arrangement of two possibilities, one is, the water King's ID has not been continuous, the middle is interrupted, in this case, water King's ID must be in the first and last, the second case, , the method is to first set the first ID as Water king, and then compare with the following ID, if equal, let a counter plus one, otherwise let the next ID set to water king, so that the number of occurrences of the most frequently found out the ID. Third, the experimental code
1 classZSW2 {3      Public Static voidMain (string[] args)4     {5         //int[] arr = {5,1,5,23,5,4,5,6,5,7,5};//Water King ID not adjacent6         int[] arr = {15,5,23,5,5,5,4,5,6,5,7,5};7         intc = 0; 8         intSW = 0;9          for(inti=0;i<arr.length;i++)Ten         { One             if(c==0)                 A             { -sw=Arr[i];  -C++; the             } -             Else -             { -                 if(Sw==arr[i])//if the preset water King ID is the same as the current ID, +C + +;//The counter adds a -                 Else +c--;//if not equal, when it is reduced to 0, the current ID is used as the water King . A             } at         }         -SYSTEM.OUT.PRINTLN (SW);//The most frequently occurring ID output -     } -}

Four, the experiment

Five, experience
Find Water King This example, is a very typical example, there are a lot of thinking, the best thing is to put all the IDs in order, with the help of the Water King's post volume
is more than half, so one-second must be the water king, but in class, the teacher means that can not be sorted, only once traversed, the water king to find out
, the first ID is set to water king, and then compared with the following ID, if equal, let a counter plus one, or let the next ID set to water king, this is the typical matching count problem,

Return the ID that has the most occurrences.

The Water king for classroom practice

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.