Classroom exercises-find the water king

Source: Internet
Author: User

Problem description:

• The trio designed an irrigation forum. Students of the Information School all like to communicate with each other. It is said that there is a "Water King" on the Forum. He not only likes to post, but also replies to every post sent by other IDs. It is widely reported that the number of posts of the "Water King" exceeds half of the number of posts. • If you have a list of posts (including replies) on the current forum, where the ID of the post author is also included, can you quickly find the legendary Water King? Problem Analysis: After analyzing the problem, I found that the post list is like a one-dimensional array Arry. The content of arry [0] is the poster of the 0th post. The problem can be abstracted: an array with a length of N in one dimension contains more than or equal to [n/2] + 1 with the same element (more than half of the elements are the same) to find the same element. Design Philosophy: We can use the elimination division between two elements. The specific implementation is to eliminate the two elements in the array, and the remaining elements must be the element to be found. In program implementation, operations are performed in order (in order or in reverse order), but the principle is the same. Traverse the array once. When traversing to the arry [I] elements, before the Count record, arry [0]-Arry [I-1] Number of elements after elimination in pairs (the remaining count elements must be the same ), id records the content of elements not eliminated, and the final ID is the requested element. Program code:
// 2016/5/17 # include <iostream> void main () {int I = 0; int COUNT = 0; // used to record the remainder int ID of each element in the first n of the array. // used to record ID int arry [] = {1, 2, 3, 1, 4, 1, 1, 1, 1, 8, 1, 6, 1}; // The post list. The subscript is the post ID and the content is the poster ID for (; I <sizeof (arry) /sizeof (arry [0]); I ++) {If (COUNT = 0) // If count is reduced to 0, representing the elimination of the previous I-1 IDS by two, update ID and count {id = arry [I]; Count = 1 ;} else // otherwise, it will continue to be eliminated in pairs with the next element or add the same number of elements {If (ID = arry [I]) // Add the number of identical elements {count ++;} else // remove {count -- ;}}/// output id std :: cout <"Shui Wang's ID is" <id <STD: Endl ;}

Implementation:

 Arry [] = {1, 2, 3, 1, 4, 1, 1, 1, 8, 1, 6, 1 };

Arry [] = {2, 5, 6, 6, 5, 5 };

Personal summary:

 There are many solutions to this problem. The simplest is to count the number of occurrences of each element in the array, and finally find the element with the most occurrences, however, this method is at the cost of time and space. In fact, if you can find the hidden side of the problem, it is often much simpler to use it, for example, in this example. However, this is the difficulty.

Classroom exercises-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.