Buy a book the lowest price question

Source: Internet
Author: User

A topic requirement:

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: this number discount

                                    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.

Two design ideas:

According to the rules of discount, 1 is not discounted, 2-5 by the corresponding discount rules to buy the lowest price, when the purchase of this number is greater than 5 o'clock, can be converted into purchase 5,6,7,8,9, such as 10=5+5,

12=5+8,21=5*3+6 and so on, for the 5,6,7,8,9 situation to obtain the following optimal situation:

When the number is an integer multiple of 5, press the uniform 75 percent cheapest.

When the number is 6 o'clock, the offer is 6=5+3 and the amount is 38.

When the number is 7 o'clock, the optimal scheme 7=5+2, the amount is 45.2.

When the number is 8 o'clock, the optimal scheme is 8=4+4, and the amount is 51.2.

When the number is 9 o'clock, the best solution 9=5+4, the amount is 55.6.

Therefore, the best purchase scheme can be obtained by combining the above.

Three codes

#include <iostream>using namespacestd;DoubleLow_price (Doublea) {    Doubleb; intx,y,p; P=(int) A; X=p/5; Y=p%5; if(x==0&&y==3) y=-1; Switch(y) { Case-1: b=3*8*0.9; Break;  Case 0: b=a*0.75*8; Break;  Case 1: b= (A-1)*8*0.75+8; Break;  Case 2: b= (A-2)*8*0.75+0.95*2*8; Break;;  Case 3: b= (A-8)*0.75*8+8*0.8*8; Break;  Case 4: b= (A-4)*8*0.75+0.8*4*8; Break; }    returnb;}voidMain () {Doubleb; cout<<"Please enter the number of purchase books:"; CIN>>A; b=Low_price (a); cout<<"the best rate is:"<<b<<Endl;}


Four

Five summary

I see this topic, first think of the enumeration, with the backward enumeration, I found the law, all the situation can be discussed separately, formed a complete set of design ideas, the problem exercise their own logical thinking ability.

Buy a book the lowest price question

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.