#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; }
时间: 2024-11-05 21:55:58