The problem of buying books in classroom work

Source: Internet
Author: User

1. Questions

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. Design algorithms can calculate the lowest price that a reader buys a batch of books.

2. Design ideas:

  For the purchase of 5 books below, it is the cheapest to buy according to the package (that is, one volume to buy one), and for 5 to 10, in addition to 8 books, the rest of the purchase of a set, then buy a set of incomplete (such as 9, buy 5 plus 4) the cheapest.

For 8, it is the cheapest to buy two sets of 4 books, for 10 or more, then 5 to 10 would have been bought, such as 11=5+6,13=5+8,20=5+5+5+5.

3. Code

#include <iostream>using namespace Std;void main () {int x,a;double y;cout<< "Enter the number of books to buy"; Cin>>x;if (x 5==0) {y= (x/5*40*0.75);} else if (x%5==1) {y= (x/5*40*0.75+8);} else if (x%5==2) {y= (x/5*40*0.75+16*0.95);} else if (x%5==3&&x!=3) {y= (x/5-1) *40*0.75+32*0.8*2;} else if (x%5==3&&x==3) {y=24*0.9;} else if (x%5==4) {y= (x/5*40*0.75+32*0.8);} Cout<<y;}

 4.: 

5. Summary

This problem needs to be carefully enumerated, found the law, found the law and then clear the idea. It is not difficult to write a program.

The problem of buying books in classroom work

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.