A topic:
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.
Two design ideas
1=1 6=5+1
2=2 7=5+2
3=3 8=4+4
4=4 9=5+4
5=5 10=5+5
........................
2.1 According to the above cycle so the use of the method to calculate the most appropriate price.
2.2 In this algorithm, the purchase of 8 is more special, the 4+4 combination is the most suitable, that is, buy two sets of 4 different books.
2.3 The specific calculation of the price is in 5 one cycle (five different discounts maximum),
2.4 The remainder of this number is calculated according to different books, the sum of which is the most appropriate price.
Three Code implementations
Package for Water king; import Java.util.inputmismatchexception;import Java.util.Scanner; Public classGS { Public Static voidMain (string[] args) { for(;;)//Infinite loop According to user demand { intnum=0, judge=0;DoublePrice=0; Scannerinch=NewScanner (System.inch); System. out. println ("Please enter the number of books to buy:"); Try //Capturing input Errors{num=inch. Nextint (); } Catch(inputmismatchexception e) {System. out. println ("The input is illegal! Please enter an integer! "); Judge=1; } inttemp=num/5; if(num%Ten==1) { price=temp*5*8*0.75+8; } Else if(num%Ten==2) { price=temp*5*8*0.75+8*2*0.95; } Else if(num%Ten==3) { price=temp*5*8*0.75+8*3*0.9; }Else if(num%Ten==4) { price=temp*5*8*0.75+8*4*0.8; }Else if(num%Ten==5) { price=temp*5*8*0.75; }Else if(num%Ten==6) { price=temp*5*8*0.75+8; }Else if(num%Ten==7) { price=temp*5*8*0.75+8*2*0.95; }Else if(num%Ten==8) { price=temp*4*8*0.8+8*4*0.8; }Else if(num%Ten==9) { price=temp*5*8*0.75+8*4*0.8; }Else if(num%Ten==0) { price=num*8*0.75; } if(judge!=1) System. out. println ("the lowest price is:"+Price ); } }}
View Code
Four experiments
8 copies
19 copies
999 copies
Summary of five persons
This rule can be found, but the idea of considering the problem is not comprehensive enough, the programming ability is insufficient.
Series of books for promotional activities