1. Design Topics
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%
2. Design Requirements
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.
Requires the design idea, code implementation, implementation, personal summary in the form of blog post. (Due date 2015-4-15 18:00)
3. Design Ideas
First of all, to be able to determine the number of purchases of 5, the book is the cheapest to buy different volumes;
Then, more than 5 of the needs of the two cases
The first kind: 6~9, they need to consider each quantity under, buy the cheapest, for example: 6 need to find the cheapest in 1, 5, 2, 6, 3, 3, other as above.
The second type: greater than 9, for example, 21, divided into 5,5,5,6. Is the first few is 5, the last one is a 6-9 of this number, according to the above situation can easily find out the maximum discount of this situation.
Code implementation:
1#include <iostream.h>2 intMain ()3 {4 inti,d;5 Doublea[ $];//type of book6a[1]=8;7a[2]=8*2*(1-0.05);8a[3]=8*3*(1-0.10);9a[4]=8*4*(1-0.20);Tena[5]=8*5*(1-0.25); One Do{ A -cout<<"Please enter the number of books to buy:"; - intnum; the -Cin>>num; - - + Doublemax[Ten]; -max[1]=a[1]+a[5]; +max[2]=a[2]+a[4]; Amax[3]=a[3]+a[3]; ata[6]=max[1]; - for(i=1;i<4; i++) - { - if(a[6]<Max[i]) - { -a[6]=Max[i]; in } - } to + - Doublemax4=a[2]+a[5]; the Doublemax5=a[3]+a[4]; *a[7]= (MAX4>MAX5)?max5:max4; $ Panax Notoginseng Doublemax6=a[3]+a[5]; - Doublemax7=a[4]*2; thea[8]= (MAX6>MAX7)?max7:max6; + A thea[9]=a[4]+a[5]; + - if(num>9) $ { $ intB= (num/5)-1; - intm=5+num%5; - thea[num]=b*a[5]+A[m]; - Wuyi the } - Wucout<<"the total cost of buying these books is:"<<a[num]<<"Yuan! "<<Endl; -cout<<"If you need to calculate the price of the other quantity please lose 1, otherwise 0:"; AboutCin>>D; $cout<<Endl; -} while(d==1); - - return 0; A}
Program:
Classroom Practice--Bookstore discount issues