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.
Requirements:The design ideas, code implementation, implementation, personal summary, in the form of blog post.
First, design ideasThe number of books purchased by the keyboard is NUM, just consider the case of Num single digit 6,7,8,9, in other cases, can be calculated directly according to the discount given in the topic. Assuming NUM's single-digit number is k, it is possible to cycle from i=5, cycle through a i--,i<k/2, and to end the cycle at the cost of (k-5) * Discount +i* discount. Second, source code #include<iostream.h>
Double Discount (int type)
{
Double k; Value after the discount is made
Switch (type)
{
Case 1:k=8;break;
Case 2:k=2*8*0.95;break;
Case 3:k=3*8*0.9;break;
Case 4:k=4*8*0.8;break;
Case 5:k=5*8*0.75;break;
Default:break;
}
return k;
}
int main ()
{
int n; Number of books
int m=0; Get the number of whole 10
int i;
Double cheap; Minimum discount
Double cost[10]={0}; Discounted prices
Double Price; The final lowest price
cout<< "Please enter purchase quantity:" <<endl;
cin>>n;
if (n>10)
{
M=N/10;
n=n%10;
}
if (n<=5)
{
Price=m*2*5*8*0.75+discount (n);
cout<< "Minimum Purchase price:" <<price<<endl;
return 0;
}
for (i=5;i>=n/2;i--)
{
Cost[i]=discount (i) +discount (n-i);
}
CHEAP=COST[5];
for (i=5;i>=n/2;i--)
{
if (Cheap>cost[i])
Cheap=cost[i];
}
Price=m*2*5*8*0.75+cheap;
cout<< "Minimum Purchase price:" <<price<<endl;
return 0;
}
iii. Results of Operation
Iv. Personal SummaryProblems encountered: When comparing the minimum values of various discounts, just compare the values that are populated into the array, and the other values do not need to participate in comparisons, and may even affect the final result. Harvest: Through this class practice, I have more rigorous programming, more careful thinking, can be found in time some minor errors.
Buy books to find out how to get discounts to make the lowest price