First, the topic requirements
The Bookstore offers a total of 5 promotions for the Harry Potter book series. volume, with number 0 , 1 , 2 , 3 , 4 said, a single reel sold at 8 Yuan, the specific discounts are 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 N design algorithm calculates the lowest price for a reader to buy a batch of books.
Requires the design idea, code implementation, implementation, personal summary in the form of blog post.
Second, the design idea
The first 5 book discount is certainly the lowest total direct buy discount. Then from 6th to 9th, the lowest price is:
Sixth book: 5 plus 1
Seventh book: 5 plus 2
Eighth book: 4 plus 4 (special)
Nineth Book: 5 plus 4
So only need to buy the book divided by 10, and then compare the remainder and these several, divided into 0~5,6~9 two kinds of large cases, write good allocation strategy. The most special nature is the time of 8, and then the price can be calculated.
Third, the code implementation
//Harry Potter Bookstore Promotion Wang Wenchi 20132980 2016/5/31#include <iostream>using namespacestd;intMain () {intnum=1, REM;//quantity and remainder floatPrice//Price floatDiscount[] = {1,1,0.95,0.9,0.8,0.75};//Discount while(num>0)//jumping out for 0 o'clock{cout<<"Please enter the number to purchase (end at 0):"; CIN>>num; REM= num%Ten;//This number is more than 10 if(rem>5)//remainder greater than 5 o'clock { if(REM = =8)//8 cheaper with 4+4{ Price= (num/Ten) *2* -+4*8*0.8*2; cout<<"Complete Purchase"<< (num/Ten) *2<<"sets,"<<"then buy two sets, each set randomly selected four different books, a total"<< Price <<"Yuan"<<Endl; } Else //other 6,7,9 are 5 books + the rest is cheaper{ Price= (num/Ten) *2+1) * -+ num%5*8* Discount[num%5]; cout<<"Complete Purchase"<< (num/Ten) *2+1<<"sets,"<<"Buy again free"<< num%5<<"this different book, a total of"<< Price <<"Yuan"<<Endl; } } Else //when the remainder is 0,1,2,3,4,5 { if(REM = =0|| REM = =5)//0,5 Buy the whole package directly{ Price= (num/5) * -; cout<<"Complete Purchase"<< num/5<<"sets, Total"<< Price <<"Yuan"<<Endl; } Else //1,2,3,4 Buy the whole set, the rest of the direct buy { if(Num >5) { price= (num/5) * -+ num%5*8* Discount[num%5]; cout<<"Complete Purchase"<< num/5<<"sets,"<<"Buy again free"<< num%5<<"this different book, a total of"<< Price <<"Yuan"<<Endl; } Else{ Price= num%5*8* Discount[num%5]; cout<<"feel free to buy"<< num%5<<"this different book, a total of"<< Price <<"Yuan"<<Endl; } }} cout<<Endl; } return 0;}
Iv. Realization OF
V. Personal SUMMARY
In fact, this time is not so difficult to imagine, as long as the rule, all the problems will be solved.
Harry Potter Bookstore Promotions