· 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.
Design ideas:
the first 5 book discount is definitely the lowest discount for all purchases. And then from the 6th to the 9th. Sixth book: 5 plus 1Seventh book: 5 plus 2Eighth book: 4 plus 4nineth Book: 5 plus 4so just divide the book you need to buy by dividing it by 5, and then compare the remainder with 5 and the few, the most special natural is 8, and then in the calculation.
Code implementation:
ImportJava.util.Scanner;PublicClassBuybook {PublicStaticvoidMain (string[] args) {//TODO auto-generated Method Stub @SuppressWarnings ("Resource") Scanner sc =NewScanner (system.in); System.out.println ("Please enter the number of books to buy:");int booknumber =Sc.nextint ();int num=booknumber%5;//Except for 5 to find the remainderDouble price=0;Switch(num) {Case 0://The number of purchases is just a multiple of 5 price=booknumber*8*0.75;Break;Case 1://The number of purchases is more than 5 1 price= (booknumber-1) *8*0.75+8;Break;Case 2://The number of purchases is more than 5 2 price= (booknumber-2) *8*0.75+2*8*0.95; Case 3:// Buy quantity in addition to 5 + 3 if (booknumber==3) {price=3*8*0.9;} else {price= (booknumber-8) *8*0.75+2*4*8*0.8;} break; Case 4:// Buy quantity in addition to 5 + 4 price= (booknumber-4) *8*0.75+4*8*0.8; break;} System.out.println ("The lowest price for the book is" + Prices);}}
implementation:
Personal Summary:
This is the owner if the idea of thinking in place on the line, find a solution to the algorithm to do. Each case in the switch statement forgets to break in the process of sitting, so the calculation is wrong.
"Harry Potter" minimum discount