Software Engineering--classroom practice "Finding water king"

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 idea if the ID table is ordered, because the water king ID exceeds the total number of stickers, the N/2 item in the ordered table must be the ID of the water king, the complexity of the algorithm is O (n*n) If you want time complexity of O (n), you can traverse the ID table, 22 adjacent ID to compare, if different, delete, Then in the remaining ID table the water king's ID number still exceeds the total of half, repeating this process, the total number of IDs in the list is reduced, the last remaining is definitely the water King's ID. Second, the source code
1  PackageCom.java.lianxi;2 3  Public classLianxi7 {4      Public Static voidMain (String arg[])5     {6         int[] ids = {1,2,3,1,1,1,2,2,2,4,1,1,1,3,3,1,1,1}; 7System.out.println ("Water King's ID is" +Findshuiwang (IDs)); 8     }9      Public Static intFindshuiwang (int[] ID)Ten     {             One         intn =id.length;  A         intShuiwang = 0;  -         intCount=0;  -          for(inti=0;i<n;i++) the         {   -             if(count = = 0) -             {   -Shuiwang =Id[i];  +Count = 1;  -             }   +             Else A             {   at                 if(Shuiwang = =Id[i]) -Count + +;  -                 Else    -Count--;  -             }   -         }   in         returnShuiwang;  -       }   to}

Three

Iv. Experience

Get this problem, I just started to think of is to make calendar all ID, and count each ID, but not easy to implement, to use the structure, and then the teacher mentioned the traversal and then sort, the middle is definitely the water King ID, but time complexity for n*n, in order to reduce the complexity of time, the teacher tips with the idea of eliminating music, Then we thought about how to solve it. A problem has a good variety of solutions, we should broaden our thinking, and then more practice to write procedures, which can be born skillfully.

Software Engineering--classroom practice "Finding water king"

Related Article

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.