Questions about buying a book

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

Second, the idea

According to the core problem of analyzing this algorithm is to solve the purchase method of 6-9 books. 1-5 you can enjoy the best discount if you buy it by one set. 10 book above can be reduced to 10 books below the situation, such as buy 11 and buy 6 is the same. Calculated by:

At 6 Ben 5+1,

At 7 Ben 5+2,

8 Ben 4+4

9 Ben 5+4

is the most preferential.

Third, the Code

1#include <iostream.h>2 voidMain ()3 {4  while5     inti,num,money,j;6cout<<"Please enter the number of books to buy:";7Cin>>num;8i=num%5;9j=num/5;Ten     Switch(i) One     { A          Case 0: -money=j* -; -              Break; the          Case 1: -money=j* -+8; -              Break; -          Case 2: +money=j* -+2*8*0.95; -              Break; +          Case 3: A             if(j=0) at             { -money=3*8*0.9; -                  Break; -             } -             Else -             { inMoney= (J-1)* -+4*8*0.8*2; -                  Break; to             } +          Case 4: -money=j* -+4*8*0.8; the              Break; *     } $cout<<"minimum consumption is"<<money<<"Block Money"<<Endl;Panax Notoginseng}

Four

V. Summary

Before writing this program to analyze his algorithm, so the idea is very clear and smooth, so the algorithm is very important.

Questions about buying a book

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.