First, the topic
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, design ideas
1. Divided into 5 cases: quantitative modulus 5 to find the remainder of each case, the optimal algorithm, the number can be.
Third, the source code
//Donglongyan Date: 2015.04.15#include <iostream.h>intMain () {DoubleMoney ; intnum,m; cout<<"********** purchase book discount calculation applet ***********"<<Endl; cout<<Endl; cout<<"Please enter number of purchases num:"<<Endl; CIN>>num; cout<<Endl; M=num%5; M=m+5; Switch(m) { Case 5: Money=num*8*0.75; Break; Case 6: Money= (num-1)*8*0.75+8; Break; Case 7: Money= (num-2)*8*0.75+8*2*0.95; Break; Case 8: Money= (num-8)*8*0.75+4*8*0.8*2; Break; Case 9: Money= (num-4)*8*0.75+8*4*0.8; Break; } cout<<"most favorable total book price:"<<money<<"Yuan"<<Endl;}
Iv. Results of operation
Five, experimental experience
This is a math problem is not a procedural problem, to understand the algorithm, it is very simple. Just don't get it wrong. The specific algorithm of 5 cases, the result is right.
Discount Book price calculation---c++--software engineering