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:
When you buy 1-5 books, you can get a discount of the original price, 5%, 10%, 20%, 25% respectively.
When buying 6-10 books, purchase a five copy, and then purchase the remaining books at the respective discount. (Buy 8 book, purchase two four copies)
When purchasing more than 10, can be decomposed, such as 11 is two five, one, 17 is three five of this, two a copy of the
Source
ImportJava.util.Scanner; Public classBook { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsScanner sc=NewScanner (system.in); System.out.println ("Please enter the number of purchase books"); intn=Sc.nextint (); intI=0; DoubleP=0; I=n%5; I=i+5; Switch(i) { Case5: P=n*8*0.75; Break; Case6: P= (n-1) *8*0.75+8; Break; Case7: P= (n-2) *8*0.75+8*2*0.95; Break; Case8: P= (n-8) *8*0.75+8*4*0.8*2; Break; Case9: P= (n-4) *8*0.75+8*4*0.8; Break; } System.out.println ("The lowest price to buy a book is" +p); } }
Personal Summary
Like this and math-related procedures, we must first enumerate the various situations, to find the law, so it can be a lot easier
Classroom Practice-Buy books with the lowest discount