Classroom Practice-Buy books with the lowest discount

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.