The design algorithm calculates the lowest price for the reader to buy a batch of books

Source: Internet
Author: User

First, the topic requirements

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 number2 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. (Due date 2015-4-15 night 18:00)second, the design ideaafter the analysis of the topic in class, found that when the purchase of the book, according to the promotion can be purchased at the lowest price; when buying 6~9 Ben, in addition to buy 8 this case is 4, 4 originally bought as the lowest price, the rest as long as buy 5 is first can calculate the lowest price, so cent (number%5+ 5) is equal to 82 cases to consider the design program, if equal to 8 sum=2*4*8*0.8+ (m-1) *8*0.75, wherein M-1 because M=NUMBER/5 here contains 8 of a 5; otherwise, as long as the purchase of 5 is calculated first, then n=number%5 for 1~ 4 in turn, add calculation. Third,
#include <iostream.h>void main () {int number;//order quantity int m;//m=number/5int n;//n=number%5 double sum=0;    cout<< "Bookstore for the Harry Potter series of books to promote activities, a total of 5 volumes, with numbers 0, 1, 2, 3, 4," said <<endl;    cout<< "A single volume of 8 yuan, the specific discount is as follows:" <<endl;    cout<< "Discount in this number" <<endl;    cout<< "2 5%" <<endl;    cout<< "3 10%" <<endl;    cout<< "4 20%" <<endl; cout<< "5 25%" <<endl;cout<< "singular book will only correspond to one discount rule, for example, two volumes purchased 1, one Volume 2, you can enjoy 5% discount, another volume is not    Enjoy the discount, the design algorithm can calculate the reader to buy a batch of books the lowest price ";    cout<<endl;    cout<< "Please enter the number of books the reader wants to buy:" <<endl;    cin>>number;    M=NUMBER/5; N=number%5;if (number==3) {sum=8*3*0.9;} Else{if ((n+5) ==8) {sum=2*4*8*0.8+ (m-1) *8*5*0.75;} Else{switch (n) {case 0:sum=m*40*0.75;break;case 1:sum=m*40*0.75+8;break;case 2:sum=m*40*0.75+16*0.95;break;case 4:  Sum=m*40*0.75+32*0.8;break;}} } cout<< "Readers buy a batch of books at the lowest priceLattice is: "<<endl; cout<<sum<< "Yuan" &LT;&LT;ENDL;}

  

Four

For 13, this should be 5.4 Ben. 4 buy

For 18 should be 5, 5, 4, 4 buy

v. Summaryas long as the serious analysis of the problem requirements and the existence of the law, you can use a very concise algorithm design procedures, once again feel the importance of the algorithm for the program.  

The design algorithm calculates the lowest price for the reader to buy a batch of books

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.