Personal projects-buying books

Source: Internet
Author: User

Buy a book with a discount at the lowest price

First, the procedure 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 procedure design thought

List all the cases that buy 10 books, find the rule: do not discount when buying a book, the price is 8; Buy two book when the lowest price is 15.2 (+); Buy three book at the lowest price of 21.6 (1+1+1); Buy four book at the lowest price is 25.6 (1+1+1+1) Buy five, the lowest price is 30 (1+1+1+1+1), buy six, the lowest price is 38 (5+1), buy seven book at the time of the lowest price is 45.2 (5+1+1), buy eight book the lowest price is 51.2 (4+4); Buy nine book at the time the lowest price is 55.6 (5+4) When buying 10 copies, the lowest price is 60 (5+5). As can be seen from the above: when buying 8 books, the lowest price is not (5+3), so every 10 books is a cycle. The total number of books divided by 10 for the remainder of the quotient: the remaining portion of 10 per cent of the number of calculations; According to this analogy, calculate the lowest price.

Third, the source program

//Li Qiao 2016,6,2//calculate the lowest price for a book based on the discount#include<iostream>using namespacestd;intMain () {intNum//Number of Books    DoubleMoney//Amount of money after folding    inti; cout<<"Please enter the number of books to buy:"; CIN>>num;  while(Num <=0)    {        //exit (1);cout <<"Please re-enter the number of books you want to buy (number must be positive):"; CIN>>num; } I= num%Ten; Switch(i) { Case 0: Money= num/5* +*0.75;  Break;  Case 1: Money= num/5* +*0.75+8;  Break;  Case 2: Money= num/5* +*0.75+8*2*0.95;  Break;  Case 3: Money= num/5* +*0.75+8*3*0.9;  Break;  Case 4: Money= num/5* +*0.75+8*4*0.8;  Break;  Case 5: Money= num/5* +*0.75+8*5*0.75;  Break;  Case 6: Money= num/5* +*0.75+8*5*0.75+8;  Break;  Case 7: Money= num/5* +*0.75+8*5*0.75+8*2*0.95;  Break;  Case 8: Money= num/5* +*0.75+8*4*0.8*2;  Break;  Case 9: Money= num/5* +*0.75+8*5*0.75+8*4*0.8;  Break; } cout<<"The discounted price of the book is:"<<money<<Endl; return 0;}

Iv. Results

Five, experience

In class, the teacher guides us: According to the problem set, enumerate various situations and calculate various possible results, after comparison to get the lowest price; and then from the purchase of 10 of the number of books, each of the lowest price to find the law, but because of their own self-confidence, the results of the calculation is not correct, failed to find the law; --When the total number of books is more than 10, it can be calculated according to the calculation method of 10, and the purchase of 8 books is a special case.

In terms of implementation, compared to the previous two classroom exercises, this time is relatively simple, the key lies in the thinking of the problem solving and the law found from it. This exercise, the heart is a little excited, because the topic is not too complex, can be their own understanding of the form of the program, but the excitement, or some worry, worry about ability can not improve, later encountered problems or can not solve. Through this experiment, I reviewed a number of common basic grammar points, will be more proficient in the future.

Personal projects-buying 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.