Buy Book Promotion questions

Source: Internet
Author: User

First, the 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.
Requires the design idea, code implementation, implementation, personal summary in the form of blog post.

Second, design ideas

Obviously, the difficulty of the problem is to buy five or more when there are different buying methods. It is not difficult to see, in buying 10 and above, can be regarded as 6,7,8,9+5x, so the problem is concentrated in buying 6,7,8,9 time. After the calculation, in addition to buy 8, 4+4 is cheaper, the others are in addition to 5 to take the remainder. Use the switch function to easily resolve.

Third, source code #include<iostream.h>
int main ()
{
Double sum;
sum=0;
int i,j;
cout<< "Please enter the number you want to buy:" <<endl;
cin>>i;
j=i%5;
Switch (j)
{

Case 0:
sum=i*8*0.75;
Break
Case 1:
Sum= (i-1) *8*0.75+8;
Break
Case 2:
Sum= (i-2) *8*0.75+15.2;
Break
Case 3:
Sum= (i-8) *8*0.75+51.2;
Break
Case 4:
Sum= (i-4) *8*0.75+25.6;
Break
}
cout<< "Best Price:" <<sum<<endl;
}

Four

V. Summary

After this experiment, I found that a good idea can greatly simplify the programming work, the idea is very important.

Buy Book Promotion questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.