Calculate the sum of the numbers 1, 2, 5 and 100.

Source: Internet
Author: User

Write a program that requires the function: Calculate the sum of the three numbers in different combinations with 1, 2, 5 and 100.
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.
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 above program has to cycle 100*50*20 times in total, and the efficiency is too low.
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
Perform a loop on Z. The possible values of X are as follows:

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.
Double ---- 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. Many large companies, such as Microsoft, adopt this method: In the debugging stage, when some important algorithms are required to implement programs, and such good algorithms are complicated, at the same time, it is easy to use an algorithm to verify this program. If the results obtained by the two algorithms are inconsistent (and the most likely algorithm to come up with is correct ), it indicates that the optimization algorithm is faulty and needs to be modified.
For example:

#ifdef DEBUGint simple();#end ifint optimize();......in a function:{result=optimize();ASSERT(result==simple());}

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.
**************************************** *************************** ***************

# Include <iostream> # include <cmath> using namespace STD; int simplesolution (); int optimize (); void main () {int number1, number2; cout <"Calculate the sum of the numbers 1, 2, 5 and 100" <Endl; number1 = simplesolution (); cout <"simple solution: "<number1 <Endl; number2 = optimize (); cout <" optimize solution: "<number2 <Endl;} int simplesolution () {int number (0); For (INT I = 0; I <= 100; I ++) for (Int J = 0; j <= 50; j ++) for (int K = 0; k <= 20; k ++) {if (I + 2 * j + 5 * k = 100) number ++ ;} return number;} int optimize () {int number = 0; For (INT m = 0; m <= 100; m ++ = 5) {number ++ = (m + 2) /2;} return number ;}

Reprinted: http://hi.baidu.com/ustc10/blog/item/197ec5a8bdc503b4cb130c5b.html

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.