1. Title:
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 2 of 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.
2. Design ideas:
If the book purchased is less than or equal to 5, then how many of the books you buy are not the same. If it is larger than 5, you need to consider it. For example, 6, is 3 3 discount, or 5 1 discount and so on. Every 5 is a loop, so just take 6 to 10. Of these, 6 7 9 are 5 and the remainder is the most economical. While 8 is 4 4 the cheapest, so this is an exception that needs to be considered.
#include <iostream>using namespace Std;int main () {int num; Double money; cout<< "Please enter the book to buy Book:"; cin>>num; if (num<=0) {cout<< "Please re-enter the number of books to buy:"; } else if (num>0) {int m=num/5; int n=num%5; if (m==0) {if (n==0) {money=0; } else if (n==1) {money=8; } else if (n==2) {money=8*2* (1-0.05); } else if (n==3) {money=8*3* (1-0.10); } else if (n==4) {money=8*4* (1-0.20); }} else if (m>0) {if (n==0) {money=5*m*8* (1-0.25); } else if (n==1) {money=5*8*m* (1-0.25) +8; } else if (n==2) {money=5*8*m* (1-0.25) +8*2* (1-0.05); } else if (n==3) {money=5*8* (m-1) * (1-0.25) +8*4*2* (1-0.20); } else if (n==4) {money=5*8*m* (1-0.25) +8*4* (1-0.20); }}} cout<< "Lowest price is:" <<money<< "meta" <<endl; return 0;}
Software engineering work-the Best Buy book problem