1. Questions
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. Design algorithms can calculate the lowest price that a reader buys a batch of books.
2. Design ideas:
For the purchase of 5 books below, it is the cheapest to buy according to the package (that is, one volume to buy one), and for 5 to 10, in addition to 8 books, the rest of the purchase of a set, then buy a set of incomplete (such as 9, buy 5 plus 4) the cheapest.
For 8, it is the cheapest to buy two sets of 4 books, for 10 or more, then 5 to 10 would have been bought, such as 11=5+6,13=5+8,20=5+5+5+5.
3. Code
#include <iostream>using namespace Std;void main () {int x,a;double y;cout<< "Enter the number of books to buy"; Cin>>x;if (x 5==0) {y= (x/5*40*0.75);} else if (x%5==1) {y= (x/5*40*0.75+8);} else if (x%5==2) {y= (x/5*40*0.75+16*0.95);} else if (x%5==3&&x!=3) {y= (x/5-1) *40*0.75+32*0.8*2;} else if (x%5==3&&x==3) {y=24*0.9;} else if (x%5==4) {y= (x/5*40*0.75+32*0.8);} Cout<<y;}
4.:
5. Summary
This problem needs to be carefully enumerated, found the law, found the law and then clear the idea. It is not difficult to write a program.
The problem of buying books in classroom work