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.
Analysis:
By enumerating the books purchased in 8 This is going to be a special case, 4, 4, combination for the cheapest case, and the other case is 5+x cheapest
Source:
#include <iostream>using namespacestd;intMain () {floatPrice//Amount intNum//Purchase Quantity intnum_s;//In addition to 10 of the business intnum_y;//remainder in addition to 10cout<<"Please enter the quantity purchased:"<<Endl; CIN>>num; num_s=num/5; Num_y=num%5; if(num_y==0)//The remainder is 0, which is the integer multiple of 5,{ Price=num_s*5*8*0.75; } Else if(num_y==1) { price=num_s*5*8*0.75+num_y*8; } Else if(num_y==2) { price=num_s*5*8*0.75+num_y*8*0.95; } Else if(num_y==3)//The remainder is 3 o'clock, there is a special case, take out a 5, and 3 together into two 4; { if(num_s==0) { price=3*8*0.1; } Else{ Price= (num_s-1)*5*8*0.75+4*8*0.8*2; } } Else{ Price=num_s*5*8*0.75+num_y*8*0.8; } cout<<price<<Endl; }
Run:
Buy a book and ask for the best discount