6

买书促销求价格最优_51CTO博客_买书的价格

 8 months ago
source link: https://blog.51cto.com/u_16503920/9200107
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

买书促销求价格最优

精选 原创

wx659baf6d4e157 2024-01-11 15:31:04 博主文章分类:大学随笔 ©著作权

文章标签 ios ci #include 文章分类 Python 后端开发 阅读数208

问题描述:

       在节假日的时候,书店一般都会做促销活动。由于《哈利波特》系列相当畅销,店长决定通过促销活动来回馈读者。在销售的《哈利波特》平装本系列中, 一共有五 卷,用编号0, 1, 2, 3, 4来表示。假设每一卷单独销售均需要8欧元。如果读者一次购买不同的两卷,就可以扣除5%的费用,三卷则更多。假设具体折扣的情况如下:

 在一份订单中,根据购买的卷数以及本书,就会出现可以应用不同折扣规则的情况。但是,一本书只会应用一个折扣规则。比如,读者一共买了两本卷一, 一本卷二。那么,可以享受到5%的折扣。另外一本卷一则不能享受折扣。如果有多种折扣,希望能够计算出的总额尽可能的低。要求根据这样的需求,设计出算 法,能够计算出读者所购买一批书的最低价格。

设计思路:

     当我们试着把具体的数字算出来之后,会发现一些规律:当总数超过10本之后那么它们的优惠选择方案与1至10本之间一样。小于5本不用说,6本划分为5+1,7为5+2,8为4+4,9为5+4,10为5+5。

     在个数不为8的情况下:先5本5本考虑,剩下的格外一起考虑。

     个数为8的情况下:总数减去8之外为5的整数按5本打折,剩下的8本按4+4.

具体实现:

#include<iostream>
using namespace std;

void main()
{
    int n;
    double  Price;
    cout << "输入买书的总本数:" << endl;
    cin >> n;
    if (n !=0)
    {
        if (n % 10 == 0 || n % 10 == 5)
            Price = 8*0.75 * n;
        else if (n % 10 == 1 || n % 10 == 6)
            Price = 6 * (n-1)+8;
        else if (n % 10 == 2 || n % 10 == 7)
            Price = 6 * (n-2)+16*0.95;
        else if (n % 10 == 3)
            Price = 6 * (n - 3) + 24 * 0.9;
        else if (n % 10 == 4 || n % 10 == 9)
            Price = 6 * (n - 4) + 32 * 0.8;    
        else if (n % 10 == 8)
            Price = 6 * (n - 8) + 64 * 0.8;
    }
    else
    {        
        cout << "输入有错。" << endl;
    }
    cout << "最合理的价格为:" << Price << endl;
}

实验结果:

买书促销求价格最优_ci
买书促销求价格最优_ios_02
买书促销求价格最优_#include_03
买书促销求价格最优_#include_04
买书促销求价格最优_ios_05

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK