First, design ideas
When the number of books to buy more than five, want to make the cheapest price, it must be as far as possible to make the book into a set of books, that is, can be continuous sets, so that can be guaranteed to enjoy more discount concessions, so:
When buying 6 books, choose the purchase method is: Five this set, plus a single book, 5+1;
When buying 7 books, the choice of purchase method is: 5+2;
When buying 8 books, the choice of purchase method is: 4+4;
When buying 9 books, the choice of purchase method is: 5+4;
When more than 10 books are available, you can use the method above, and so on.
Second, the program code
1 ImportJava.util.*;2 classBargain3 {4 Public Static voidMain (string[] args)5 {6 Doublesum = 0; 7System.out.println ("Please enter the number you want to buy:");8Scanner sc =NewScanner (system.in);9 intnum =sc.nextint ();Ten One if(num<=5)//no more than five copies of the purchase book A { - Switch(num) - { the Case1: -sum=8;//No discount - Break; - Case2: +sum=2*8*0.95;//95 percent - Break; + Case3: Asum=3*8*0.9;//90 percent at Break; - Case4: -sum=4*8*0.8;//80 percent - Break; - Case5: -sum=5*8*0.75;//75 percent in Break; - } to } + if(num>5&&num<=9) - { thesum=method (num); * } $ if(num>=10)//more than 10 books boughtPanax Notoginseng { - Switch(num%5) the { + Case0: ASum= (NUM/5) *8*5*0.75; the Break; + Case1: -Sum= (num/5-1) *8*5*0.75+method (6); $ Break; $ Case2: -Sum= (num/5-1) *8*5*0.75+method (7); - Break; the Case3: -Sum= (num/5-1) *8*5*0.75+method (8);Wuyi Break; the Case4: -Sum= (num/5-1) *8*5*0.75+method (9); Wu Break; - } About } $ -System.out.println ("The cheapest price is:" +sum+ "Yuan"); - } - A + Public Static DoubleMethodintA//The number of books to buy is 6, 7, 8, 9 the { - DoubleMin = 0; $ inttemp = a%5; the Switch(temp) the { the Case1: theMin=5*8*0.75+8; - Break; in Case2: themin=5*8*0.75+2*8*0.95; the Break; About Case3: themin=4*8*0.8+4*8*0.8; the Break; the Case4: +min=5*8*0.75+4*8*0.8; - Break; the }Bayi returnmin; the } the}
Iii. Results
Iv. New Experience
In thinking of this idea, at the beginning of this thinking is very complex, think that the need to consider the book, but also to determine the type of books, but after testing a few, found that there is no need to consider this, you can directly consider the purchase of books can be made into a set of books, so that the natural can get the lowest price, But when the modulus 5 is 8 of the case, is more special, the last two or to be divided into 4+4, so there is no problem
Buy books for class practice at a discounted price