[Beauty of programming] book purchasing-Dynamic Planning

Source: Internet
Author: User

I. Problems

There are a total of five flags in the "Harry Potter" series. Assume that each volume is sold separately for 8 euros. If you purchase two different volumes at a time, you can deduct 5% of the fee, and the three volumes are more. Assume that the discount is as follows:

2 discount 5%

3 discounts: 10%

4 discounts: 20%

5 discounts: 25%

Problem: An algorithm is designed to calculate the lowest price for a batch of books purchased by the reader.

 

Ii. Problem Analysis:

Dynamic Planning, greedy algorithms, and branch limitation are all about optimization !! Until the optimal solution is found !!

Greedy Policy

When the number of books is n <5, the books are directly purchased at a discount.

When the number of books is n> 5, the situation is as follows:

In this case, we can cite the situation of each combination, and analyze any situation (I, J, K, M, N ).

First, find 5 different types of books in all books. If yes, purchase at the discount price of 5 books.

Next, find out all four types of books in the remaining books. If yes, purchase at the discount price of the four books.

Find out all three types of books in the remaining books. If yes, purchase at the discount price of the three books.

Finally, find all the two types of books in the remaining books. If yes, purchase them at the discount price of the two books.

The remaining books are bought at full price.

If there is an inverse example using this method (greedy method), for example, when you buy 8 books, you can split it into 5 + 3, with a discount of 1.55, or 4 + 4, A discount of 1.6 is included in both cases. The first case can be ruled out by selecting the lowest discount.

Conclusion: The greedy policy is not desirable.

Dynamic Planning

To solve the problem with dynamic planning, you must first find the recursive formula of dynamic planning, because dynamic planning is a layer-by-layer recursion from top to bottom, and then a layer-by-layer solution from bottom to bottom! Finally, the final result is obtained based on the bottom-layer conclusions.

The price of the five-volume books is the same as 8 euro, so the effect of purchasing (, 0, 0) is the same as that of (, 0. Here, we can simplify the process to make it easier to discuss how to increase or decrease the number of books you have bought.

The parameters to be processed are the number of purchased volumes, so recursion must be related to these five parameters. You can sort the parameters in ascending order. The number of parameters not 0 is discussed to find all possible discount types. Then, the minimum price is obtained from the current discount type.

(X1, x2, X3, X4, X5) indicates the number of purchased volumes. F (x1, x2, X3, X4, X5) indicates the lowest price. X1
<X2 <X3 <X4 <X5

F (x1, x2, X3, X4, X5) = 0
; When all parameters are 0 (this is also the exit to exit recursion)

F (x1, x2, X3, X4, X5) =
Min {

5*8 * (1-25%) + f (X1-1, X2-1, X3-1, X4-1, X5-1)
// All parameters> 0


4*8 * (1-20%) + f (x1, X2-1, X3-1, X4-1)
// X2> 0

3*8 * (1-10%) + f (x1, x2, X3-1, X4-1, X5-1)
// X3> 0


2*8 * (1-5%) + f (x1, x2, X3, X4-1, X5-1)
// X4> 0


8 + f (x1, x2, X3, X4, X5-1)
// X5> 0

}

3. Dynamic Planning source code:

Source code:

# Include <iostream> # include <cstdlib> # include <cstdio> using namespace STD; const int larg = 100000; // defines a maximum value, template <typename T> void rerank (t m [], int length), which is equivalent to the value of this position when the minimum value is obtained. // Bubble Sorting {for (INT I = length-1; i> 0; I --) {for (Int J = 0; j <I; j ++) {If (M [J]> M [J + 1]) {T temp; temp = m [J + 1]; m [J + 1] = m [J]; m [J] = temp ;}}}} double min (double A, double B, double C, double D, double E) // returns the minimum value {double NN [5] = {A, B, C, D, e}; rerank (NN, 5); Return NN [0];} double find_bestsol (INT X1, int X2, int X3, int X4, int X5) // Dynamic Planning (Recursive Implementation) {int N [5] = {x1, x2, X3, X4, X5}; rerank (n, 5 ); // sort N in ascending order. X1 = N [0]; x2 = N [1]; X3 = N [2]; X4 = N [3]; x5 = N [4];/* X1 <X2 <X3 <X4 <X5 */If (N [0]> 0) // the smallest value is greater than 0; the number of all the volumes is greater than 0, and all possible discounts are listed. The minimum value {return min (8.0 + find_bestsol (x1, x2, X3, X4, X5-1) is returned ), 2x8*0.95 + find_bestsol (x1, x2, X3, X4-1, X5-1), 3x8*0.9 + find_bestsol (x1, x2, X3-1, x4-1, X5-1), 4*8*0.8 + find_bestsol (x1, x2-1, X3-1, X4-1, X5-1 ), 5*8*0.75 + find_bestsol (x1-1, x2-1, X3-1, X4-1, X5-1 ));} else if (n [0] = 0) & (N [1]> 0) {return min (8.0 + find_bestsol (x1, x2, X3, X4, x5-1), 2x8*0.95 + find_bestsol (x1, x2, X3, X4-1, X5-1), 3x8*0.9 + find_bestsol (x1, x2, x3-1, X4-1, X5-1), 4x8*0.8 + find_bestsol (x1, x2-1, X3-1, X4-1, X5-1 ), larg);} else if (n [0] = 0) & (N [1] = 0) & (N [2]> 0 )) {return min (8.0 + find_bestsol (x1, x2, X3, X4, X5-1), 2*8*0.95 + find_bestsol (x1, x2, X3, X4-1, x5-1), 3*8*0.9 + find_bestsol (x1, x2, X3-1, X4-1, X5-1), Larg, Larg );} else if (n [0] = 0) & (N [1] = 0) & (N [2] = 0) & (N [3]> 0) {return min (8.0 + find_bestsol (x1, x2, X3, X4, X5-1 ), 2*8*0.95 + find_bestsol (x1, x2, X3, X4-1, X5-1), Larg );} else if (n [0] = 0) & (N [1] = 0) & (N [2] = 0) & (N [3] = 0) & (N [4]> 0) {return 8.0 + find_bestsol (x1, x2, X3, X4, x5-1);} else // All are 0 {return 0;} int main () {// int N1 [5] = {3, 4, 2, 1, 0 }; int N1 [5] = {2, 2, 1, 1}; double solution = find_bestsol (N1 [0], N1 [1], N1 [2], N1 [3], n1 [4]); cout <"the minimum cost is:" <solution <Endl ;}

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.