编程之美1.4 买书问题

 

// 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;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值