//买书问题,
//如果一种书籍五册,单独买一册8元,买两册不同的打95折,买三册不同的书籍9折,买四册不同的书籍8折,买五册不同书籍75折,问怎么买书最便宜。
//比如买2本一册,2本2册,2本三册,1本4册,1本5册
//那么最优打折方式就是:分两次购买,一本一册,一本二册,一本三册和一本四册,然后就是剩下的书籍
//此问题用到动态规划,
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//默认x1>x2>x3>x4>x5;
double money(int x1,int x2,int x3,int x4,int x5){
if(x1 < x2 || x2 < x3 || x3 < x4 || x4 < x5){//保证满足我们的默认情况
vector<int> tmp;
tmp.push_back(x1);
tmp.push_back(x2);
tmp.push_back(x3);
tmp.push_back(x4);
tmp.push_back(x5);
sort(tmp.begin(),tmp.end());
x1 = tmp[4];
x2 = tmp[3];
x3 = tmp[2];
x4 = tmp[1];
x5 = tmp[0];
money(x1,x2,x3,x4,x5);
}
if(x1 == 0)
return 0;
double res1,res2,res3,res4,res5;
res1 = res2 = res3 = res4 = res5 = 0;
if(x5 >= 1)
res5 = 5 * 8 * (1 - 0.25) + money(x1 - 1, x2 - 1,x3 - 1, x4 - 1, x5 - 1);
if(x4 >= 1)
res4 = 4 * 8 * (1 - 0.2) + money(x1 - 1, x2 - 1,x3 - 1, x4 - 1, x5);
if(x3 >= 1)
res3 = 3 * 8 * (1 - 0.1) + money(x1 - 1, x2 - 1,x3 - 1, x4, x5);
if(x2 >= 1)
res2 = 2 * 8 * (1 - 0.05) + money(x1 - 1, x2 - 1,x3, x4, x5);
if(x1 >= 1)
res1 = 8 + money(x1 - 1, x2, x3, x4, x5);
if(x2 == 0)
return res1;
if(x3 == 0)
return min(res1,res2);
if(x4 == 0)
return min(res1,min(res2,res3));
if(x5 == 0)
return min(res1,min(res2,min(res3,res4)));
if(x5 > 0)
return min(res1,min(res2,min(res3,min(res4,res5))));
}
int main()
{
cout<<money(2,2,2,1,1);
cin.get();
return 0;
}