First, design ideas
Title Requirements:
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: this number discount
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.
Design ideas:
According to this discount rule, 1 does not discount, 2-5 of the time according to the corresponding discount rules to purchase, at 5 above the time, can be converted to buy 5,6,7,8,9 book to calculate, the corresponding preferential scheme and prices are as follows:
When the number of books is exactly 5 multiples, the unified 75 percent calculation;
When the number of books is 6 o'clock, the preferential scheme is 1+5, the total price is: 38;
When the number of books is 7 o'clock, the preferential scheme is 2+5, the total price is: 45.2;
When the number of books is 8 o'clock, the preferential scheme is 4+3, the total price is: 51.2;
When the number of books is 9 o'clock, the preferential scheme is 4+5, the total price is: 55.6;
In the purchase of more than 5 books in the range, for example, buy 13 books, preferential scheme for 5+8; When buying 44 books, the preferential scheme is 35+9, this is the best book proposal.
Second, the program source code
1//Maishu.cpp:Defines the entry point for the console application.2//34 #include"StdAfx.h"5 #include"Iostream.h"67Double Add (IntNumber8{9IntM10DoubleSumOne m = number%3 m5;12Switch(m)13{14Case6://Buy 6 books (5+1)sum =38;16Break;17Case7://Buy 7 books (5+2)sum =45.2;19Break;20Case8://Buy 8 Books (4+4)sum =51.2;22Break;23Case9://Buy 9 Books (4+5)sum =55.6;25Break;26Case5://Buy 5 books (5)sum =30;28Break;29}30ReturnSum31}32int main (int argc,char*Argv[])33{34Double sum =0,d;//Sum indicates the total price of the purchase book35int Number,a, b, C;//Number indicates the quantity of books purchased36//A = Number/5,b = * (A-1), D is an intermediate variable37IntXuanze;cout<<"Purchase Quantity:";Cin>>Number40if (number <=0)41{cout<<"Input Error!"<<Endl43Return0;44}45Elseif (number <=5)46{47Switch(number)48{49Case1:sum =8;51Break;52Case2:sum =8**0.95;54Break;55Case3:Sum of sums =8** *0.9;57Break;58Case4:sum =8*The0.8;60Break;61Case5:sum =8*5*0.75;63Break;64}65}66Else67{68 a = number/5;69 B = 30 * (A-170 d = add (number) D; }73 cout<< Payment amount: "<<sum<<endl;return 0;75}
Third, the operation result screenshot
Iv. Total and experience
in this request the most preferential purchase book price of the procedure design, I for test instructions used enumerated way directly to buy 6,7,8,9 book the most preferential Total price, and did not use a certain algorithm to achieve this solution process, although reached the final goal, but when the discount scheme changes, is not applicable, and needs to be improved.
The most preferential book price programming