First, the topic requirements
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.
Second, the idea
According to the core problem of analyzing this algorithm is to solve the purchase method of 6-9 books. 1-5 you can enjoy the best discount if you buy it by one set. 10 book above can be reduced to 10 books below the situation, such as buy 11 and buy 6 is the same. Calculated by:
At 6 Ben 5+1,
At 7 Ben 5+2,
8 Ben 4+4
9 Ben 5+4
is the most preferential.
Third, the Code
1#include <iostream.h>2 voidMain ()3 {4 while5 inti,num,money,j;6cout<<"Please enter the number of books to buy:";7Cin>>num;8i=num%5;9j=num/5;Ten Switch(i) One { A Case 0: -money=j* -; - Break; the Case 1: -money=j* -+8; - Break; - Case 2: +money=j* -+2*8*0.95; - Break; + Case 3: A if(j=0) at { -money=3*8*0.9; - Break; - } - Else - { inMoney= (J-1)* -+4*8*0.8*2; - Break; to } + Case 4: -money=j* -+4*8*0.8; the Break; * } $cout<<"minimum consumption is"<<money<<"Block Money"<<Endl;Panax Notoginseng}
Four
V. Summary
Before writing this program to analyze his algorithm, so the idea is very clear and smooth, so the algorithm is very important.
Questions about buying a book