Title: Bookstore for the "Harry Potter" series of books for promotional activities, a total of 5 volumes, with numbers 0, 1, 2, 3, 4, a single volume of 8 yuan, the specific discount is as follows:
Discount on this number
2 5%
3 10%
4 20%
5 25%
Depending on the number of volumes purchased and this number, different discount rules will be applicable. The singular book only corresponds to one discount rule, for example, two volume 1, one volume 2, you can enjoy 5% discount, another volume does not enjoy the discount.
The design algorithm calculates the lowest price for the reader to buy a batch of books.
Ideas:
If the number of books is a multiple of 10, it is easy to see, divided into two sets of 5 is the cheapest, so only to consider the remaining single (1~9) case, and the minimum discount of the book has been given, then the analysis of 6~9.
Each of these cases is enumerated:
(6 copies)
1 ben +5: = 38 yuan (lowest)
2 ben +4: = 40.8 yuan
3 ben +3: = 43.2 yuan
(7 copies)
2 ben +5: = 45.2 yuan (lowest)
3 ben +4: = 47.2 yuan
(8 copies)
3 ben +5: = 51.6 yuan
4 ben +4: = 51.2 yuan (lowest)
(9 copies)
4 ben +5: = 55.6 yuan (lowest)
As you can see, the other three are a combination of 5, except that the 8 is the most cost-effective combination of two 4.
Source:
Operation Result:
Summarize:
After a period of time, and continue to do classroom exercises, fortunately no rusty.
Analysis of the topic, the first thought of the idea is written earlier, and did not consider what algorithm, but in the discount situation a lot of all the classification must be very troublesome, which requires to find the law, design a suitable algorithm. In class, listening to the speeches of several students, the algorithm of one of the students impressed very deep, the same is considered the remaining single-book discount rate. 2 (0.1), 3 (0.3), 4 (0.8), 5 (1.25), see combination and maximum. There is a special case than what you think, and this method does not exist in Terry.
Find the difference between yourself and others, analyze the process, find the deficiency.
Bookstore promotion Activities