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.
Requires the design idea, code implementation, implementation, personal summary in the form of blog post.
Second, design ideas
Obviously, the difficulty of the problem is to buy five or more when there are different buying methods. It is not difficult to see, in buying 10 and above, can be regarded as 6,7,8,9+5x, so the problem is concentrated in buying 6,7,8,9 time. After the calculation, in addition to buy 8, 4+4 is cheaper, the others are in addition to 5 to take the remainder. Use the switch function to easily resolve.
Third, source code #include<iostream.h>
int main ()
{
Double sum;
sum=0;
int i,j;
cout<< "Please enter the number you want to buy:" <<endl;
cin>>i;
j=i%5;
Switch (j)
{
Case 0:
sum=i*8*0.75;
Break
Case 1:
Sum= (i-1) *8*0.75+8;
Break
Case 2:
Sum= (i-2) *8*0.75+15.2;
Break
Case 3:
Sum= (i-8) *8*0.75+51.2;
Break
Case 4:
Sum= (i-4) *8*0.75+25.6;
Break
}
cout<< "Best Price:" <<sum<<endl;
}
Four
V. Summary
After this experiment, I found that a good idea can greatly simplify the programming work, the idea is very important.
Buy Book Promotion questions