Question:
During holidays, bookstores generally offer promotions. Since the Harry Potter series is quite popular, the store manager decided to reward readers through promotions. In the sales of the Harry Potter series, there are a total of five volumes, represented by numbers 0, 1, 2, 3, 4. 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:
Discounts: 2 5% 3 10% 4 20% 5 25% in a single order, depending on the number of purchased volumes and the book, different discount rules can be applied. However, a book only applies one discount rule. For example, the reader buys two copies, one for the second. You can enjoy a 5% discount. You cannot enjoy a discount for another volume. If there are multiple discounts, we hope to calculate the total amount as low as possible. According to this requirement, design Algorithm To calculate the lowest price for a batch of books purchased by the reader.
Analysis:
First, assume that the five books areA, B, C, D, E,General assumptions are not lost.Na> = NB> = NC> = Nd> = ne
All books can be dividedKGroup,No duplicate books in each group,
The optimal solution has the following properties::
1All groups that contain only one book only contain the same book
If two groups contain different books,Then the two combinations and,Better Solution,Conflict with the optimal solution.
2A group that contains a book only containsA
IfInclude booksA ',IfNa' = Na,ThenA,'Reconciliation
IfNa' <Na,A group must containADoes not containA ',OnlyA'To be better understood.
3All groups that contain only two books contain the same two books
Group(A', B '), (A', C ')Discount0.2Restructured(A', B ', C ')And()Discount0.3
4The group containing the two books must containA
If oneAGroup,The group of the two books does not containA,The merge operation can be better understood.
If no copy existsAGroup,The two groups containA', '',Contains three books,This group is mandatoryAAnd missingA'By
(A', A'), (bytes ),Discount0.4Worse(A'), (axya ') 0.8
(A', A'), (axyz)Discount0.9Worse(A'), (axyza') 1.25
5The group containing the two books must containB
Same as above
6All groups containing only three books contain the same three books
(A, B, C), (a, B, d)Discount0.6, (A, B), (A, B, C, D)Discount0.9
7All groups that contain three books containA
If onlyAGroup,Better Combination
If only A, B (A, B) (XYZ) discount 0.4, (B) (axyz) discount 0.8
If one does not exist,Group 2,Assume that the three books are(X, y, z ),It must containAThe four groups are missingX, (x, y, z) (a, ''')Discount1.1,Worse
(Y, Z), (a, A', X)Discount1.35
8All three groups containB, c
Same as above
9All groups that contain four booksA, B, c
SetXyzwIs
(ABC) (bcde)Discount1.1,Worse(B, c), (a, B, c, d, e)Discount1.35
Other situations are also verifiable
10Five books and three books will not appear at the same time.
(A, B, C), (A, B, C, D, E)Discount1.55,Worse(A, B, C, D), (A, B, C, E)Discount1.6
The following conclusions can be drawn from the above proofs:
- Each group containsA,Number of all groups andASame
- All groups that contain two or more versions includeB,Number of groups andBSame
- All groups that contain three or more includeC,Number of groups andCSame
- 3.,5. Coexistence
The solution is as follows:
Code Const Double Buybook: unit_price = 8 ;
Const Double Buybook: discounts [ 5 ] = { 1 , 0.95 , 0.9 , 0.8 , 0.75 };
Static Const Int Book_kinds = 5 ;
Double Buybook: searchfast ( Int * Books)
{
Sort (books );
Int G [ 5 ];
G [ 0 ] = Books [ 0 ] - Books [ 1 ];
G [ 1 ] = Books [ 1 ] - Books [ 2 ];
G [ 2 ] = Books [ 2 ] - Books [ 3 ];
G [ 3 ] = Books [ 3 ] - Books [ 4 ];
G [ 4 ] = Books [ 4 ];
Int T = Min (G [ 2 ], G [ 4 ]);
If (T > 0 )
{
G [ 2 ] -= T;
G [ 4 ] -= T;
G [ 3 ] + = 2 * T;
}
Double Sum = 0 ;
For ( Int I = 0 ; I < Book_kinds; ++ I)
{
Sum + = G [I] * (I + 1 ) * Unit_price * Discounts [I];
}
Return SUM;
}