The beauty of programming 2.3 search for the poster king, the beauty 2.3

Source: Internet
Author: User

The beauty of programming 2.3 search for the poster king, the beauty 2.3

Because this question is not easy to write test cases, you can convert the question to: In an array, a number appears more than half the size of the array, this is the same as the original meaning of the question.

The idea of this question is that we remove two different numbers from the array at the same time, so the original laws in the array remain unchanged (only for this question ).

Okay, let's give the function declaration first:

/* 2.3 search for the poster master */bool DutVerify (int *, int, int); int DutFindNumMoreThanHalf (int *, int );


We can see that there is one more in the function declaration: DutVerify. The function is used for verification, that is, we can find a number in an array not empty. However, we cannot explain that the number of occurrences of this number is more than half the length of the array. Therefore, considering the robustness of the algorithm, we still need to verify it at last.

Since the comments have been written in the Code, there is not much to explain here. paste the Code:

/* Verify the valid function */bool DutVerify (int * A, int size, int result) {/* number of occurrences */int count = 0; for (int I = 0; I <size; ++ I) {if (A [I] = result) ++ count ;} /* verify whether the number of occurrences is greater than half the length of the array */if (count> (size/2) return true; elsereturn false ;} /* This question is converted to an array with a certain number, this number is more than half the number of elements in the array * // The global variable identifies whether the input parameter is valid */bool _ DutFindNumMoreThanHalf = false; int DutFindNumMoreThanHalf (int *, int size) {if (! A | size <= 0) {_ DutFindNumMoreThanHalf = true; return-1 ;}int result; int count = 0; /** the idea here is to remove two different numbers at the same time, so the "prototype" of the array will not change, * That is, the number greater than half is still more than half of the current number in the current array */for (int I = 0; I <size; ++ I) {if (count = 0) {result = A [I]; ++ count;} else {if (result = A [I]) ++ count; else -- count ;}/ * after obtaining this number, we need to verify whether the number appears more than half of the total number */bool res = DutVerify (A, size, result); if (res = true) return result; else {_ DutFindNumMoreThanHalf = true; return-1 ;}}






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.