How to use mathematical ideas to solve the 1/2/5 combination Problem

Source: Internet
Author: User

Huawei pen exam: write a program that requires the function: Find the sum of the three numbers in different combinations with 100, and 5. For example, the first 100 is a combination, and the fifth one and the 19th five are a combination ....

Answer: The easiest algorithm to come up with is:

Set X to the number of 1, Y to the number of 2, Z to the number of 5, and number to the combination.

X + 2 * Y + 5 * z = 100 number of solutions to this equation

Note that 0 <= x <=, 0 <= Y <= 50, 0 <= z = 20, so you can program it:

Number = 0;

For (x = 0; X <= 100; X ++)

For (y = 0; y <= 50; y ++)

For (Z = 0; Z <= 20; Z ++)

If (x + 2 * Y + 5 * z) = 100)

Number ++;

Cout <number <Endl;

The easiest to think of is the triplicate loop, which loops 100*50*20 times and is the most inefficient algorithm.

In fact, this question is an obvious mathematical problem, not simply a programming problem. My solution is as follows:

Because X + 2y + 5z = 100

So X + 2y = 100-5z, and z <= 20 x <= 100 Y <= 50

So (x + 2y) <= 100, and (x + 5z) is an even number

Apply a loop to Z (Because Z has the smallest range and the cycle optimization problem, the number of loops in the outermost layer is small to avoid excessive flow breaks ), the possible value of X is as follows (the number of X values represents the number of final solutions. For a Z, after X is set, y automatically determines ):

Z = 0, x = 100, 98, 96,... 0

Z = 1, x = 95, 93,..., 1

Z = 2, x = 90, 88,..., 0

Z = 3, x = 85, 83,..., 1

Z = 4, x = 80, 78,..., 0

......

Z = 19, x = 5, 3, 1

Z = 20, x = 0

Therefore, the total number of combinations is an even number less than 100 + an odd number less than 95 + an even number less than 90 + an odd number less than... + 5 + 1,

That is:

(51 + 48) + (46 + 43) + (41 + 38) + (36 + 33) + (31 + 28) + (26 + 23) + (21 + 18) + (16 + 13) + (11 + 8) + (6 + 3) + 1

The number of even numbers (including 0) within an even m can be expressed as M/2 + 1 = (m + 2)/2

An odd number within an odd m can also be expressed as (m + 2)/2

Therefore, the total number of combinations can be programmed as follows:

Number = 0;

For (INT m = 0; m <= 100; m + = 5)

{

Number + = (m + 2)/2;

}

Cout <number <Endl;

This program only needs to loop 21 times and two variables to get the answer, which is much more efficient than the above program-just because of some simple mathematical analysis.

This once again proves that computer programs = data structures + algorithms, and algorithms are the soul of the program. When software is used to implement any engineering problems, you must choose to meet the current resource restrictions, optimal Algorithms under various constraints, such as user requirements and development time. Instead, you must use the easiest algorithm to compile a program immediately. This is not the behavior of a professional R & D personnel.

So, isn't that easy-to-think algorithm completely useless? No, this algorithm can be used to verify the correctness of the new algorithm. It is very useful in the debugging phase. In the debugging phase, when some important algorithms are required to implement the program, and such good algorithms are complicated, the program is verified with easy-to-think algorithms, if the results of the two algorithms are inconsistent (and the algorithm that is most likely to come up with is correct), it indicates that the optimization algorithm is faulty and needs to be modified.

For example:

# Ifdef debug // release version will automatically cancel simple compilation, Conditional compilation, and reduce code space

Int simple ();

# End if

Int optimize ();

......

In a function:

{

Result = optimize ();

Assert (result = simple (); // The assert macro is valid only in the debug version. The release version is empty.

}

In this way, in the debugging phase, if the results of the simple algorithm and the optimization algorithm are inconsistent, the assertion will be played. At the same time, the released version of the program does not contain the bulky simple () function. Any large-scale engineering software requires a well-designed debugging method, which is a useful method.

A typical debugging method is to display printed information in the Linux kernel module. Global display levels are used to control the printing of 7 types of messages.

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.