码迷,mamicode.com
首页 > 其他好文 > 详细

求最低价格

时间:2016-06-23 14:22:51      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
using namespace std;
#define PRICE 8
double discount[5]={0,0.05,0.1,0.2,0.25};

double Caculate(int num1,int num2)        //计算该情况的价钱
{
    double money,money1,money2;
    money1=(PRICE-PRICE*discount[num1-1])*num1;
    money2=(PRICE-PRICE*discount[num2-1])*num2;
    money=money1+money2;
    return money;
}
double Discount(int num)            //分情况讨论价钱
{
    int k,yushu;
    double money1,money2,min;
    k=num/5;                    //取整
    yushu=num%5;                //取余
    min=num*PRICE;
    switch(k)
    {
        case 0:
            money1=(PRICE-PRICE*discount[num-1])*num;
            break;
        default:
            if(yushu==0)
            {
                money1=(PRICE-PRICE*discount[4])*num;
                return money1;
            }
            for(int i=1;i<(yushu+5);i++)
            {
                money1=Caculate(i,(yushu+5)-i);
                if(money1<min)
                    min=money1;
            }
            if(num>9)
                money2=(k-1)*5*(PRICE-PRICE*discount[4]);
            else
                money2=0;
            return min+money2;
    }
    return money1;
}
int main(int argc, char* argv[])
{
    int booknum;
    double money;
    cout<<"请输入购买书籍的数量:";
    cin>>booknum;
    money=Discount(booknum);
    cout<<"最优惠的价格是"<<money<<""<<endl;
    return 0;
}

技术分享

求最低价格

标签:

原文地址:http://www.cnblogs.com/lq897897/p/5610590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!