First, the experimental 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.
Second, the design idea
First 1-5, according to the above discount to buy the most affordable. 6-7, according to 5+1, 2+5 buy the most affordable, 8 o'clock according to 4+4 buy the most affordable, 9-10 when 5+4, 5+5 the most affordable. And so on, by the law of the topic 10 is the cycle.
Third, the experimental code
1 ImportJava.util.Scanner;2 Public classBuybook {3 Public Static voidMain (string[] args) {4 //TODO auto-generated Method Stub5Book B =NewBook ();6 B.setbooknumber ();7 B.getbookway ();8 }9 Ten } One class Book A { - intNumber=-1; - inttime1;//Number of Cycles the inttime2; - DoubleMoney//the most affordable money required - - voidSetbooknumber ()//Enter the number of books you need to buy + { -System.out.println ("Please enter the number of books to be purchased"); + while(number<0)//input value cannot be less than 0 A { atScanner L =NewScanner (system.in); -Number =l.nextint (); - } - - } - in voidGetbookway ()//get the best deals on how to buy books - { totime1 = NUMBER/10; +Time2 = number-time1*10; - if(Time2 = = 0) the { *Money = time1* (8*5*0.75*2) +0; $ }Panax Notoginseng Else if(Time2 = = 1) - { theMoney = time1* (8*5*0.75*2) +1*8*1; + } A Else if(Time2 = = 2) the { +Money = time1* (8*5*0.75*2) +2*8*0.95; - } $ Else if(Time2 = = 3) $ { -Money = time1* (8*5*0.75*2) +3*8*0.9; - } the Else if(Time2 = = 4) - {WuyiMoney = time1* (8*5*0.75*2) +4*8*0.8; the } - Else if(Time2 = = 5) Wu { -Money = time1* (8*5*0.75*2) +5*8*0.75; About } $ Else if(Time2 = = 6) - { -Money = time1* (8*5*0.75*2) +5*8*0.75+8; - } A Else if(Time2 = = 7) + { theMoney = time1* (8*5*0.75*2) +5*8*0.75+2*8*0.95; - } $ Else if(Time2 = = 8) the { theMoney = time1* (8*5*0.75*2) +4*8*0.8*2; the } the Else if(Time2 = = 9) - { inMoney = time1* (8*5*0.75*2) +5*8*0.75+4*8*0.8; the } theSystem.out.println ("The lowest price is:" +Money ); About } the}
Four, the experiment:
V. Personal SUMMARY
Pay attention to the pattern of this type of problem.
Algorithm-Lowest price of the book