The first chapter of the game of music buy books problem

Source: Internet
Author: User

I. Issues

On the Ark of the "Harry Potter" paperback series, a total of five volumes. Suppose that each volume is sold separately for € 8. If the reader buys a different two volume at a time, it can deduct 5% of the cost, and three volumes will be more. Suppose the specific discounts are as follows:

This number 2 discount 5%

This number 3 discount 10%

This number 4 discount 20%

This number 5 discount 25%

Problem: The algorithm is designed to calculate the lowest price for a group of books that the reader buys.


Two. Problem Analysis:

The optimization problem is compared with dynamic programming, greedy algorithm and branch clearance until the optimal solution is found.


Three. The dynamic programming solution of the analysis on the book

To use dynamic programming solutions, you first need to find the recursive formula for dynamic programming, because dynamic planning is recursive from top to bottom layer, and then the bottom-up layer answers. Finally, the final results are solved according to the underlying conclusion.

The price of a five-volume book is equal to 8 euros, so the purchase (1,0,0,0,0) is the same as the (0,1,0,0,0) effect. This can be simplified to allow the purchased book to increase (decrement) in accordance with this book, thus facilitating discussion.

The parameters to be processed are the number of purchases for each volume, so recursion must be related to these five parameters. The parameters can be sorted in order from small to large. Discusses the number of arguments that are not 0, thus finding all possible types of discounts. The lowest price is then taken from the current discount category.

(X1,X2,X3,X4,X5) represents the number of purchases per volume, and F (X1,X2,X3,X4,X5) represents 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 from recursion)

F (x1,x2,x3,x4,x5) = min{

5*8* (1-25%) +f (x1-1,x2-1,x3-1,x4-1,x5-1)//Parameters all > 0

4*8* (1-20%) +f (x1,x2-1,x3-1,x4-1,x5-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

}

Four. Using dynamic programming to solve the optimal solution

#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace Std;
const int LARG = 100000;//defines a maximum value that ignores the value of this position when the minimum value is taken


Template <typename t>
void Rerank (T m[],int length)/bubble sort (can be optimized here)
{
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)//return 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 programming (Recursive implementation)
{
int n[5] ={x1, x2, X3, X4, X5};
Rerank (n,5);//N to a small to large sort
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 is greater than 0; all volumes are greater than 0; then list all possible discounts and return the minimum value
{
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),
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),
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),
4 * 8 * 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, Larg, 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);
}
The else//are all 0.
{
return 0;
}
}

int main ()
{
int n1[5] = {3,4,2,1,0};
int n1[5] = {2,2,2,1,1};
Double solution = Find_bestsol (n1[0],n1[1],n1[2],n1[3],n1[4]);
cout<< "The least money spent 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.