Pair development----Buy book problem

Source: Internet
Author: User

Group members: Zhao Yonghen, Van Dey

First, the experimental topic

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.
Requires the design idea, code implementation, implementation, personal summary in the form of blog post.

Second, design ideas

When we first saw this problem, our idea was to discuss the situation, for example, when we say 12, the program automatically divides it into many kinds of situations, such as 4.4.4, 3.4.5 and so on. But in the design of the program we found that the implementation is very cumbersome or can not be achieved, because the larger the number of cases, the more we need to discuss the more, can not achieve a large number of sub-case discussion. Later through the calculation, we found that all the cases can be divided into 5 categories, that is, divisible by 5 after the remainder of the discussion, for the total price, we also categorized 5 formulas can be solved, so no matter how much can be brought in and very concise.

Third, the source code

#include <iostream.h>intMain () {intnum; intShang; intYushu; DoublePrice ; cout<<"Please enter the number of books to buy:"; CIN>>num; Shang=num/5; Yushu=num%5; Switch(Yushu) { Case 0: Price= -*Shang; cout<<"the total price of the book is:"<<price<<"Yuan"<<Endl;  Break;  Case 1: Price= -*shang+8; cout<<"the total price of the book is:"<<price<<"Yuan"<<Endl;  Break;  Case 2: Price= -*shang+15.2; cout<<"the total price of the book is:"<<price<<"Yuan"<<Endl;  Break;  Case 3: Price= -* (shang-1)+51.2; cout<<"the total price of the book is:"<<price<<"Yuan"<<Endl;  Break;  Case 4: Price= -*shang+25.6; cout<<"the total price of the book is:"<<price<<"Yuan"<<Endl;  Break; }    return 0;}

Four, the operation

V. Summary of the Experiment

Just see this topic feeling a bit difficult, but after and partner research, found that seemingly complex problems can be divided into a few small groups, and then each group analysis, so that the problem is resolved. Many problems in itself is not difficult, we are always by its appearance to frighten, calm analysis only found in their own ability within the scope.

Pair development----Buy book problem

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.