最小费用购物

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

struct Commodity {
	int piece;//数量
	int price;//价格
};
const int MAXCODE = 999;//商品编码的最大值
const int SALECOMB  =99;//优惠商品组合数
const int KIND = 5;//商品种类
const int QUANTITY  =5;//购买某种商品数量的最大值
ifstream ifs("input.txt");
ifstream ifs_offer("offer.txt");
ofstream ofs("output.txt");

int b;//购买商品种类数
int s;//当前优惠组合数
int num[MAXCODE+1];//记录商品编码与商品种类的对应关系
int product[KIND+1];//记录不同种类商品的购买数量
int offer[SALECOMB+1][KIND+1];//offer[i][j]: 商品组合的优惠价(j=0);某种优惠组合中某种商品需要购买的数量(j>0)
Commodity purch[KIND+1];//记录不同商品的购买数量和购买价格
int cost[QUANTITY + 1][QUANTITY + 1][QUANTITY + 1][QUANTITY + 1][QUANTITY + 1];//记录本次购买的总花费
void minicost() {
	int i, j, k, m, n, p, minm;
	minm = 0;
	for (i = 1; i <= b; i++)
		minm += product[i] * purch[i].price;
	for (p = 1; p <= s; p++) {
		i = product[1] - offer[p][1];
		j = product[2] - offer[p][2];
		k = product[3] - offer[p][3];
		m = product[4] - offer[p][4];
		n = product[5] - offer[p][5];
		if (i >= 0 && j >= 0 && k >= 0 && m >= 0 && n >= 0 && cost[i][j][k][m][n] + offer[p][0] < minm)
			minm = cost[i][j][k][m][n] + offer[p][0];
	}
	cost[product[1]][product[2]][product[3]][product[4]][product[5]] = minm;
}

void init() {
	int i, j, n, p, t, code;
	ifs>>b;//读入商品种类数
	for (i = 1; i <= b; i++) {
		ifs>>code;//编码
		ifs>>purch[i].piece ;//购买该种商品总数
		ifs>>purch[i].price;//该商品的正常单价
		num[code] = i;//记录对应关系
	}
	ifs_offer>>s ;//读入s种优惠商品组合数
	for (i = 1; i <= s; i++) {
		ifs_offer>>t ;//种类数
		for (j = 1; j <= t; j++) {
			ifs_offer>>n ;//商品编码
			ifs_offer>>p ;//该商品在此组合中的数量
			offer[i][num[n]] = p;
		}
		ifs_offer>>offer[i][0] ;//最后一个数代表此商品的优惠价
	}
}

void comp(int i) {
	if (i > b) {
		minicost();
		return;
	}
	for (int j = 0; j <= purch[i].piece; j++) {
		product[i] = j;
		comp(i + 1);
	}
}

int main() {
	init();
	comp(1);
	cout<<(cost[product[1]][product[2]][product[3]][product[4]][product[5]]);
	ofs<<(cost[product[1]][product[2]][product[3]][product[4]][product[5]]);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值