// BuyBookProblem.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
const int large_num = 100000;
template <typename T>
void rerank(T *p,int length) //冒泡
{
for (int i = length-1; i > 0; --i)
{
for (int j = 0;j < i; ++j)
{
if (p[j] < p[j+1])
{
T temp;
temp = p[j];
p[j] = p[j+1];
p[j+1] = temp;
}
}
}
}
double MIN(double x1,double x2,double x3,double x4,double x5)
{
double res_num[]={x1,x2,x3,x4,x5};
rerank(res_num,5);
return res_num[4];
}
double BestSolution_Seach( int NUM1,int NUM2,int NUM3,int NUM4,int NUM5)
{
int n[5]= { NUM1,NUM2,NUM3,NUM4,NUM5 };
rerank(n,5); //对n进行从大到小排序
NUM1 = n[0];
NUM2 = n[1];
NUM3 = n[2];
NUM4 = n[3];
NUM5 = n[4];// NUM1 > NUM2 > NUM3.....
if (NUM5 > 0) //所有的都 >= 1
{
return MIN(
8.0 + BestSolution_Seach(NUM1-1,NUM2,NUM3,NUM4,NUM5),
8.0 * 2 * (1-0.05) + BestSolution_Seach(NUM1-1,NUM2-1,NUM3,NUM4,NUM5),
8.0 * 3 * (1-0.1) + BestSolution_Seach(NUM1-1,NUM2-1,NUM3-1,NUM4,NUM5),
8.0 * 4 * (1-0.2) + BestSolution_Seach(NUM1-1,NUM2-1,NUM3-1,NUM4-1,NUM5),
8.0 * 5 * (1-0.25) + BestSolution_Seach(NUM1-1,NUM2-1,NUM3-1,NUM4-1,NUM5-1)
);
}
else if ((NUM5 == 0)&&(NUM4 > 0))
{
return MIN (
8.0 + BestSolution_Seach(NUM1-1,NUM2,NUM3,NUM4,NUM5),
8.0 * 2 * (1-0.05) + BestSolution_Seach(NUM1-1,NUM2-1,NUM3,NUM4,NUM5),
8.0 * 3 * (1-0.1) + BestSolution_Seach(NUM1-1,NUM2-1,NUM3-1,NUM4,NUM5),
8.0 * 4 * (1-0.2) + BestSolution_Seach(NUM1-1,NUM2-1,NUM3-1,NUM4-1,NUM5),
large_num
);
}
else if ((NUM5 == 0)&&(NUM4 == 0)&&(NUM3 > 0))
{
return MIN (
8.0 + BestSolution_Seach(NUM1-1,NUM2,NUM3,NUM4,NUM5),
8.0 * 2 * (1-0.05) + BestSolution_Seach(NUM1-1,NUM2-1,NUM3,NUM4,NUM5),
8.0 * 3 * (1-0.1) + BestSolution_Seach(NUM1-1,NUM2-1,NUM3-1,NUM4,NUM5),
large_num,
large_num
);
}
else if ((NUM5 == 0)&&(NUM4 == 0)&&(NUM3 == 0)&&(NUM2 > 0))
{
return MIN (
8.0 + BestSolution_Seach(NUM1-1,NUM2,NUM3,NUM4,NUM5),
8.0 * 2 * (1-0.05) + BestSolution_Seach(NUM1-1,NUM2-1,NUM3,NUM4,NUM5),
large_num,
large_num,
large_num
);
}
else if ((NUM5 == 0)&&(NUM4 == 0)&&(NUM3 == 0)&&(NUM2 == 0)&&(NUM1 > 0))
{
return MIN (
8.0 + BestSolution_Seach(NUM1-1,NUM2,NUM3,NUM4,NUM5),
large_num,
large_num,
large_num,
large_num
);
}
else //没买书
{
return 0;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int m[] = { 3,6,8,3,1,1};
double best_soul;
best_soul = BestSolution_Seach(m[0],m[1],m[2],m[3],m[4]);
cout << "购买这批书的最低价格" << best_soul << endl;
return 0;
}