First, the topic
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.
Requires the design idea, code implementation, implementation, personal summary in the form of blog post.
Second, the design idea
Through the analysis of this topic, find out the law for the remainder of the problem, we can first find 5,6,7,8,9 four group of the best plan, the rest of the data Mod5 plus 5, but also 5,6,7,8,9 these situations, you can get the best solution.
Third, the Code
1#include"stdio.h"2 intMain ()3 {4 intBnum,num,num1,jixu;5 DoubleMoney ;6a1:printf ("Please enter the number of books:");7scanf"%d",&bnum);8num1=bnum/5-1;9num=bnum%5+5;Ten Switch(num) One { A Case 5: -money=5*8*0.75; - Break; the Case 6: -money=5*8*0.75+8; - Break; - Case 7: +money=5*8*0.75+2*8*0.95; - Break; + Case 8: Amoney=4*8*0.8*2; at Break; - Case 9: -money=5*8*0.75+4*8*0.8; - Break; - } -Money=money+num1*5*8*0.75; inprintf"The Total price of the book you purchased:%g\n", money); -printf"do you want to continue? Continue please enter 1, exit input 0\n"); toscanf"%d",&Jixu); + Switch(Jixu) - { the Case 0: *printf"Welcome to use next time! \ n"); $ Break;Panax Notoginseng Case 1: - GotoA1; the Break; + } A return 0; the}
View Code
Four, the experiment
Five, experimental experience
A lot of problems, we want to find the law, especially the infinite problem, and we have to think about in the design, such as before I did not design loop input link, each use must reopen, very unfriendly.
Promotion of book Promotions