First, the topic 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:
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. Second, the key to design ideas is to solve the problem when purchasing 6, 7, 8, 9 should be how to buy. After calculation, 6 time: 1 Ben +5 is the best; 7 time: 2 This +5 is the best; 8 this time: 4 This +4 is the best; 9 this time: 4 This +5 is the best. 1-5 this time without special consideration, the other number when the conversion to 6-9 of the case processing. This number is about 10 to be redundant, then converted to 6-9 of the case. Third, the Code
1#include <iostream.h>2 3 voidShowintXDouble&sum)4 {5 if(x==6)6 {7cout<<"5 This group, 1 a group to buy";8sum=sum+5*8*0.75+8;9 }Ten if(x==7) One { Acout<<"5 This group, 2 a group to buy"; -sum=sum+5*8*0.75+2*8*0.95; - } the if(x==8) - { -cout<<"4 This group, 4 a group to buy"; -sum=sum+8*8*0.8; + } - if(x==9) + { Acout<<"5 This group, 4 a group to buy"; atsum=sum+5*8*0.75+4*8*0.8; - } - } - - voidMain () - { incout<<"Please enter the number you want to buy:"<<Endl; - intnum,a,b,i; to Doublesum=0; +Cin>>num; -cout<<"The most favorable solution is:"<<Endl; thea=num%Ten; * if(a>5) $ {Panax Notoginsengb=num/5; - for(i=1; i<=b;i++) the { +cout<<"5 Group One,"; A } thesum=sum+b*8*5*0.75; + Show (a,sum); - } $ Else if(a<5&&a>0) $ { -a=a+5; -b=num/5-1; the for(i=1; i<=b;i++) - {Wuyicout<<"5 Group One,"; the } -sum=sum+b*8*5*0.75; Wu Show (a,sum); - } About Else $ { -b=num/5; - for(i=1; i<=b;i++) - { Acout<<"5 Group One,"; + } thesum=sum+b*8*5*0.75; - } $cout<<endl<<"altogether spent"<<sum<<"Yuan"<<Endl; the}
Iv. Results of operation
V. Summary
This topic is the practice of the design of the algorithm, the beginning of the feeling with the implementation of the program can consider the traversal, enumerate all the cases and then find the minimum value, will not miss the situation. But it is more cumbersome to find, and the most time-consuming, not the most concise algorithm implementation. Then through the calculation of the purchase of 6-9 conditions found the law, the design of the algorithm.
Questions about buying a book