1. Title:
The bookstore offers promotions for the Harry Potter book series, with a total of 5 volumes, numbered 0,1,2, 3 ,
4 , the individual volume is priced at $8, and 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 will only correspond to one
Discount rules, such as the purchase of two volumes 1, one volume 2, you can enjoy 5% Discount, another volume
Not enjoy the offer. The design algorithm calculates the lowest price for the reader to buy a batch of books.
2. Design ideas:
The number of books that the user wants to buy is divided by 5 to get the remainder, and then 5 to consider separately to buy 6,7, 8 ,
9 of the time, the last to find the most discount situation. The following are the prices of the books in these cases:
When the number of books is 6 , the preferential scheme is 1+5, the total price is:
When the number of books is 7 , the preferential scheme is 2+5, the total price is:45.2;
When the number of books is 8 , the preferential scheme is 4+4, the total price is:51.2;
When the number of books is 9 , the preferential scheme is 4+5, the total price is:55.6;
3. Source code:
1#include <iostream.h>2 voidMain ()3 { 4 5 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<<"Lowest Price is"<<money<<"Yuan"<<Endl;Panax Notoginseng}
4. Running
5, feel:
In the beginning I understand the wrong test instructions, later after the teacher explained to understand, under the guidance of the teacher found a solution, so still very grateful to the teacher.
Bookstore Promotion Issues