First, title:
Bookstore for the "Harry Potter" series of books for promotional activities, a total of 5 volumes, a single volume of 8 yuan, the specific discount is as follows: This number corresponding discount is: 2 Ben -5%,3 ben -10%,4 ben -20%,5 Ben-25%. Depending on the number of volumes purchased and this number, different discount rule conditions will be applicable. A single book will only correspond to one discount rule. The design algorithm calculates the lowest price for the reader to buy a batch of books.
Second, design ideas:
For 1-5, define a double function with the return value type, listing 5 offers. For 6-9, for example 7, 2 and 5 are calculated first, then 2 and 1 are 3, 3 and 4 are calculated, and 3 and 1 are added to calculate the 4,3 of this case. Until after the addition of one less than 5. For more than 10, it's simple.
Third, the source code:
#include <iostream.h>DoubleFainti) { DoubleA; Switch(i) { Case 1: a=1.0; Break; Case 2: a=0.95; Break; Case 3: a=0.9; Break; Case 4: a=0.8; Break; Case 5: a=0.75; Break; } returnA;}intMain () {intbooknumber,keybook,a,i,d,b,g,j; Doublesum; cout<<"Input:"; CIN>>Booknumber; if(booknumber>0&&booknumber<=5) { DoubleC=fa (Booknumber); Sum=c*booknumber*8; cout<<sum; } Else if(booknumber>5&&booknumber<=9) {a=booknumber-5; DoubleD=FA (a); Sum=d*a*8+ -; for(i=a;i<5; i++) {b=booknumber-A; DoubleE=FA (a); Doublef=FA (b); if(e*a*8+f*b*8<sum) sum=e*a*8+f*b*8; } cout<<sum; } Elseg=booknumber%5; J=booknumber/5; Doublek=FA (g); Sum=j* -+k*8*G; cout<<sum;}
Four.
Five. The experiment summarizes this
This test should be said to be not too difficult, as long as 3 kinds of cases, the key is 6-7 of this situation, must take into account each situation. This can be resolved with a switch statement, and a loop. The key to the program is to abstract the actual problem into a mathematical problem re-use program implementation, as long as the thought of each situation, the solution is OK.
Software engineering class Assignment-calculating the best price