面试题目,搭配最优出售方案

题目描述
服装店新进了a条领带,b条裤子,c个帽子,d件衬衫,现在要把这些搭配起来售卖。有三种搭配方式,一条领带和一件衬衫,一条裤子和一件村衫,一个帽子和一件衬衫。卖出一套领带加村衫可以得到e元,卖出一套裤子加衬衫可以得到元,卖出一套帽子加衬衫可以得到g元。现在你需要输出最大的获利方式。
样例输入
2 3 4 5 6 7 8
样例输出
39
提示
4个帽子加4件衬衫获利32元,1条裤子加1件衬衫获利7元,一共得到39元。

开始拿到题目的时候看到有数的组合,本来想用vector<map<int,int>,int>来解决问题的,后来发现不能用迭代器类型获取到map中的数据,然后建立了一个与map结构相似的结构体替代相应的功能。代码如下

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<map>
using namespace std;

class Solution {
public:
	int mins(int x, int y) { return (x < y) ? x : y; }
	void combine() {
		cin >> a >> b >> c >> d >> e >> f >> g;
		rest.emplace_back(make_pair(mp(a, d), e));
		rest.emplace_back(make_pair(mp(b, d), f));
		rest.emplace_back(make_pair(mp(c, d), g));
	}
	int calMaxValue() {
		//降序排序,先找价值最大的
		sort(rest.begin(), rest.end(), [=](pair<struct mp,int> &t1, pair<struct mp, int> &t2) {return t1.second > t2.second; });
		for (int i=0; d > 0;i++) {
			int num = mins(rest[i].first.x, rest[i].first.y);
			maxValue += (rest[i].second)*(d>num?num:d);
			d = (d > num) ? (d - rest[i].first.x) : 0;
		}
		return maxValue;
	}
private:
	int a, b, c, d, e, f, g;
	//结构体
	struct mp {
		int x;
		int y;
		//有参构造方式
		mp(int x, int y) :x(x), y(y) {};
	};
	vector<pair<struct mp,int>> rest;
	int maxValue = 0;
};

int main() {
	Solution s1;
	s1.combine();
	int MAX = s1.calMaxValue();
	cout << "maxValue= " << MAX << endl;
	return 0;
}

在这里插入图片描述
如果有什么问题,请与我联系,指出问题所在,我一定抓紧纠正,谢谢大家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是小峰呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值