One: 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:
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.
Requires the design idea, code implementation, implementation, personal summary in the form of blog post.
Second: Design ideas
First Buy 5 is definitely the best of all purchase, and then calculate to buy 6-10, find the Best Buy book algorithm, the optimal algorithm is:
Books: Buy book Method Preferential price
6 Ben 5+1 30+8=38
7 Ben 5+2 30+15.2=45.2
8 Ben 4+4 25.6*2=51.2
9 Ben 5+4 30+25.6=55.6
10 Ben 5+5 30*2=60
So will need to buy the book divided by 10, and then the remainder is a few, according to a few ways to buy, the final addition is the best Buy book algorithm.
Code:
1 //The best algorithm for buying books2 //2016.5.313 //Guillechang4#include <iostream>5 using namespacestd;6 7 intMain ()8 {9 intNum,num_zheng,num_yu;Ten DoublePrice ; Onecout <<"Please enter the number of books to buy:"<<Endl; ACIN >>num; -Num_zheng = num/Ten; -Num_yu = num%Ten; thePrice = -*Num_zheng; - Switch(Num_yu) - { - Case 1: +Price + =8; - Break; + Case 2: APrice + =15.2; at Break; - Case 3: -Price + =21.6; - Break; - Case 4: -Price + =25.6; in Break; - Case 5: toPrice + = -; + Break; - Case 6: thePrice + = -;//5+1 * Break; $ Case 7:Panax NotoginsengPrice + =45.2;//5+2 - Break; the Case 8: +Price + =51.2;//4+4 (Special) A Break; the Case 9: +Price + =55.6;//5+4 - Break; $ } $Num_zheng = num/5; -Num_yu = num%5; - if(Num_yu = =3&&num_zheng>0)//when divided by 5 to 3 is a special case the { -cout <<"Buy Book Method: first Complete purchase"<< num_zheng-1<<"set, then buy two sets, each set randomly 4 different books. The total price is:"<< Price <<Endl;Wuyi } the Else - { Wucout <<"Buy Book Method: first Complete purchase"<< Num_zheng <<"set, and then free to buy"<<num_yu<<"a different book. The total price is:"<< Price <<Endl; - } About}
Test Case 1:
Test Case 2:
Test Case 3:
Test Case 4:
Test Case 5:
Experiment Summary:
The subject of the experiment is relatively simple, plus the teacher in the class to guide, calculate the first 10 kinds of situations, the back of the law, the overall is relatively simple.
Buy Harry Potter book algorithm