Book Discount best plan

Source: Internet
Author: User

Title: 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.

Requirement: The design algorithm can calculate the lowest price for the reader to buy a batch of books.

Design ideas: By the need to type the number of purchase required, and then by the program to provide the best purchase plan. By reading the topic carefully, can be divided into 3 types: 1, the purchase of the book when the time, the direct purchase of different book coupons can be; 2, the purchase of the book 6~9 can be obtained through a simple calculation of the best purchase plan (6: A set + single volume; Purchase 7: One set + different volumes two volumes; Purchase 8: Two volumes of 4 different volumes Buy 9: A set of 4 volumes of different volumes, 3, purchase 10 books and above, can cycle the second situation.

Program code:

#include <iostream.h>#include<math.h>intMain () {intN; cout<<"Please enter the number you want to purchase:"; CIN>>N; if(n==0) {cout<<"the best purchase plan is: buy different volumes"<<n<<"Volume"<<Endl; cout<<"The required amount for the optimal solution is"<<n*8<<"Yuan"<<Endl; }    if(n==1) {cout<<"the best purchase plan is: buy different volumes"<<n<<"Volume"<<Endl; cout<<"The required amount for the optimal solution is"<<n*8<<"Yuan"<<Endl; }    if(n==2) {cout<<"the best purchase plan is: buy different volumes"<<n<<"Volume"<<Endl; cout<<"The required amount for the optimal solution is"<<n*8*0.95<<"Yuan"<<Endl; }    if(n==3) {cout<<"the best purchase plan is: buy different volumes"<<n<<"Volume"<<Endl; cout<<"The required amount for the optimal solution is"<<n*8*0.9<<"Yuan"<<Endl; }    if(n==4) {cout<<"the best purchase plan is: buy different volumes"<<n<<"Volume"<<Endl; cout<<"The required amount for the optimal solution is"<<n*8*0.8<<"Yuan"<<Endl; }    if(n==5) {cout<<"the best purchase plan is: buy different volumes"<<n<<"Volume"<<Endl; cout<<"The required amount for the optimal solution is"<<n*8*0.75<<"Yuan"<<Endl; }    if(n==6) {cout<<"the best purchase plan is to buy a set and different volumes"<<n-5<<"Volume"<<Endl; cout<<"The required amount for the optimal solution is"<<5*8*0.75+8<<"Yuan"<<Endl; }    if(n==7) {cout<<"the best purchase plan is to buy a set and different volumes"<<n-5<<"Volume"<<Endl; cout<<"The required amount for the optimal solution is"<<5*8*0.75+ -*0.9<<"Yuan"<<Endl; }    if(n==8) {cout<<"the best purchase plan is: Purchase a set of different volumes"<<4<<"Volume"<<Endl; cout<<"The required amount for the optimal solution is"<<4*8*0.8*2<<"Yuan"<<Endl; }    if(n==9) {cout<<"the best purchase plan is to buy a set and different volumes"<<n-5<<"Volume"<<Endl; cout<<"The required amount for the optimal solution is"<<5*8*0.75+ +*0.8<<"Yuan"<<Endl; }    if(n>=Ten)    {        intj,k; J=n/5; K=n%5; if(k==0) {cout<<"the best purchase proposal is: Purchase the complete"<<j<<"Set"<<Endl; cout<<"The required amount for the optimal solution is"<<j*5*8*0.75<<"Yuan"<<Endl; }        if(k==1) {cout<<"the best purchase proposal is: Purchase the complete"<<j<<"sets, and 1 volumes of different volumes"<<Endl; cout<<"The required amount for the optimal solution is"<<j*5*8*0.75+8<<"Yuan"<<Endl; }        if(k==2) {cout<<"the best purchase proposal is: Purchase the complete"<<j<<"sets, and 2 volumes of different volumes"<<Endl; cout<<"The required amount for the optimal solution is"<<j*5*8*0.75+ -*0.9<<"Yuan"<<Endl; }        if(k==3) {cout<<"the best purchase proposal is: Purchase the complete"<<j<<"sets, and two different volumes of 4 rolls"<<Endl; cout<<"The required amount for the optimal solution is"<<j*5*8*0.75+4*8*0.8*2- +*0.75<<"Yuan"<<Endl; }        if(k==4) {cout<<"the best purchase proposal is: Purchase the complete"<<j<<"sets, and 4 volumes of different volumes"<<Endl; cout<<"The required amount for the optimal solution is"<<j*5*8*0.75+ +*0.8<<"Yuan"<<Endl; }            }    return 0;}

Program test:

Summary thinking:

In the process of editing the program, a minimum error occurred, the assignment (=) and the judgment is equal (= =) confusion, resulting in a compilation error. I will certainly pay close attention to it in the future.

Book Discount best plan

Related Article

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.