Title Requirements:
A 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.
First, design ideas
Buy this book |
Price |
1 |
S1=8 |
2 |
s2=16*0.95=15.2 |
3 |
s3=24*0.9=21.6 |
4 |
s4=32*0.8=25.6 |
5 |
S5=40*0.75=30 |
6 |
4 Ben and 2 ben s6=s4+s2=40.8 |
5 Ben and 1 Ben s6=s5+s1=38 |
3 Ben and 3 ben s6=s3+s3=43.2 |
7 |
5 Ben and 2 ben s7=s5+s2=45.2 |
4 Ben and 3 ben s7=s4+s3=47.2 |
8 |
5 Ben and 3 ben s8=s5+s3=51.6 |
4 Ben and 4 ben s8=s4+s4=51.2 |
9 |
5 Ben and 4 ben s9=s5+s4=55.6 |
As can be seen from the table above, the number of construction purchases is N:
If n<=5 is purchased directly by discount
If 5< n<10 , except for the number 8 , the rest is 5 plus n-5
If n>10 , can be divided into 10*i+k form, where k Book calculation method and the above similar.
Second, the source program
1 //Designer Letter 1201-2 Division new Red2 3#include"stdafx.h"4#include"iostream"5 using namespacestd;6 7 intMain ()8 {9 intb//define the number of books purchasedTen intM,n; One inti; A Doublesum=0; -cout<<"Please enter the number of books you need to purchase:"<<Endl; -Cin>>b; them=b/5; -n=b%5; -i=n+5; - if(i!=8) + { - Switch(n) + { A Case 0: atsum=m* +*0.75; - Break; - Case 1: -sum=m* +*0.75+8; - Break; - Case 2: insum=m* +*0.75+ -*0.95; - Break; to Case 3: +sum=m* +*0.75+ -*0.9; - Break; the Case 4: *sum=m* +*0.75+ +*0.8; $ Break;Panax Notoginseng } - } the Else +Sum= (M-1)* -+4*8*0.8*2; Acout<<"Reader Purchase"<<b<<"The lowest price for this book is:"<<Endl; thecout<<sum<<Endl; + return 0; -}
Iii. Results of operation
Iv. Summary of the experiment
This experiment I feel it is not difficult to write, the most important thing is to think about it, thought in the classroom after the teacher's tips and my classmates and I thought about how the algorithm should be realized. This time the code is not particularly cumbersome, the function is better to achieve, I am in the process of writing, is to consider buying 8 books when should deal with a little difficulty.
How to buy a batch of books to make it the lowest price