Buy a book the lowest price question

Source: Internet
Author: User

Design ideas

The following rules can be seen according to the calculation:

When buying 6 books, buying 5 and buying 1 is the lowest price.

When buying 7 books, buying 5 sets and 2 sets is the lowest price.

When buying 8 books, buying two sets of 4 is the lowest price.

When buying 9 books, buying 5 sets and 4 sets is the lowest price.

So when the number of books to buy Mol5 3 o'clock is a special case, the rest of the situation has rules to follow.

There are two cases when the remainder of 3.

The first case is to buy only 3, the direct output to buy 3 of this set of prices will be OK.

The second case is to buy 8, 13, 18 ... The number of books to buy minus 8 to buy 5 of the set price after the purchase of two sets of 4 is the lowest price.

SOURCE program code

1#include <iostream>2 using namespacestd;3 4 voidMain ()5 {6     intn,a,b;7     Doublemin;8cout<<"Please enter the number of purchase books:";9Cin>>N;Tena=n%5; One     if(a==0) A     { -min= (N-A)/5*(8*5)*0.75; -     } the     if(a==1) -     { -min= (N-A)/5*(8*5)*0.75+8; -     } +     if(a==2) -     { +min= (N-A)/5*(8*5)*0.75+15.2; A     } at     if(a==3) -     { -         if(n==3) -         { -cout<<"lowest Price is: 21.6"<<Endl; -Exit0); in         } -Min= (n8)/5*(8*5)*0.75+51.2; to     } +     if(a==4) -     { themin= (N-A)/5*(8*5)*0.75+25.6; *     } $cout<<"the lowest price is:"<<min<<Endl;Panax Notoginseng}


Run results

Programming summary

To solve this problem programming is not the primary problem, the use of mathematical methods of how to calculate is the main difficulty, first use of mathematical thinking, as long as the idea is correct, the programming process is very simple. So you need to have enough time to think about the problem before programming!

Buy a book the lowest price question

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.