The Bookstore's promotion of the Harry Potter book series
5
volume, with number
0
,
1
,
2
,
3
,
4
said, a single roll of selling price
8
Yuan,
the specific discounts are as follows:
this number
Discount
2 5%
3 10%
4 20%
5 25%
depending on the number of volumes purchased and the number of copies, different discount rules will be applicable
. The singular book only corresponds to a discount rule, for example, a two purchase
this volume
1
, a volume
2
, you can enjoy
5%
the discount, the other volume
not enjoy the offer.
The design algorithm calculates the lowest price for the reader to buy a batch of books.
1#include <iostream.h>2 intMain () {3 intnum;4 intM,n;5 floatsum;6cout<<"Please enter the book for the book you want to buy:";7Cin>>num;8m=num/5;9n=num%5;Ten if(num!=3) One { A Switch(n) - { - Case 0: thesum=num*8*0.75; - Break; - Case 1: -sum=m*5*8*0.75+8; + Break; - Case 2: +sum=m*5*8*0.75+2*8*0.95; A Break; at Case 3: -Sum= (M-1)*5*8+4*8*0.8+4*8*0.8; - Break; - Case 4: -sum=m*5*0.75+4*8*0.8; - Break; in } - } to Else +sum=3*8*0.9; -cout<<"the lowest price the reader buys for a batch of books is:"<<sum<<"Yuan"<<Endl; the return 0; *}
:
Experience: When doing this problem, I took 6-10 of the situation to calculate: when buying 6, 5 and 1 buy the cheapest, buy 7, 5 Ben and 2 buy the cheapest, buy 8, 4 and 4 buy the cheapest, buy 9, 5 and 4 are the cheapest, buy 10, 5 and 5 of the cheapest buy. Then more than 10 of the numbers we can look at as these combinations. For example, 11 can become, 5,5,1;12 become 5,5,2. But there are special circumstances, when buying this number divided by 5 o'clock, more than 3 of the case, such as 13, we can not divide him into 5,5,3, we should consider 5, 8, that is, 5,4,4, this is the cheapest. So, we can program according to the case divided by 5. The problem still tests the ability of mathematics, although the intermediate calculation took a little time, but the programming is completed smoothly.
Buying a book question