The design algorithm calculates the lowest price for the reader to buy a batch of books

Source: Internet
Author: User

First, title:
N 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 as shown in the number 2, discount 5%; This number 3, discount 10%; Ben 4, Discount 20%; the number 5, discount 25%; will correspond to different discount rule conditions. 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.

Second, the problem-solving ideas:

Through the analysis of 6 books, 7 books, 8 books, 9 books, when the number is 6, 7, 9 o'clock, the cheapest combination is 5+1,5+2,5+4, when this number is 8 o'clock, the cheapest combination is 4+4, so when this number is 13,23,33, it is necessary to separate a 8来 to be the cheapest, So there are two cases: this number divided by five is the remainder of 3 and this number divided by five is the remainder of 3.

Third, the source code:

 PackageShopp;ImportJava.util.*; Public classMain { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub                intN=0; intN=0; intM=0; Scanner S=NewScanner (system.in); System.out.print ("Please enter the number of the book you want to buy:"); N=S.nextint (); if(n%5==3) {n=n/5; M=n%5; if(n==0) {System.out.println ("The lowest price the reader buys for a batch of books is:" + (3*8*0.9)); }            Else{System.out.println ("The lowest price for readers to buy a batch of books is:" + ((n-1) *5*8*0.75+2*4*8*0.8)); }        }        Else{n=n/5; M=n%5; Switch(m) { Case0:system.out.println ("The lowest price the reader buys a batch of books is:" + (n*5*8*0.75)); Break;  Case1:system.out.println ("The lowest price the reader buys a batch of books is:" + (n*5*8*0.75+8)); Break;  Case2:system.out.println ("The lowest price the reader buys a batch of books is:" + (n*5*8*0.75+m*8*0.95)); Break;  Case4:system.out.println ("The lowest price the reader buys a batch of books is:" + (n*5*8*0.75+m*8*0.9)); Break;  Case5:system.out.println ("The lowest price the reader buys a batch of books is:" + (n*5*8*0.75+m*8*0.75)); Break; }        }    }}

Iv. operation of the program:

V. Personal SUMMARY

The overall feeling of the experiment is better, there are not too many problems, mainly in the use of switch statements, forget to add break.

The design algorithm calculates the lowest price for the reader to buy a batch of books

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.