pdd笔试题

   日期:2020-08-05     浏览:99    评论:0    
核心提示:拼多多的笔试没有报名,但昨天听伙伴们说很难,所以一共4道题,挑了2道会的,自己编了一下。#include#include#includeusing namespace std;int main(){ int K, N; cin >> K; cin >> N; vectora(N); for (int i = 0; i < N; i++)_.

拼多多提前批的笔试没有报名,但昨天听伙伴们说很难,所以一共4道题,挑了2道会的,自己编了一下。

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

int main()
{
	int K, N;
	cin >> K;
	cin >> N;
	vector<int>a(N);
	for (int i = 0; i < N; i++)
		cin >> a[i];

	int cur = K;
	int count = 0;
	int i = 0;
	for (; i < N; i++)
	{
		cur -= a[i];
		if (cur == 0)
		{
			cout << "paradox" << endl;
			break;
		}
		else if (cur < 0)
		{
			cur = -cur;
			count++;
		}
	}
	if (i == N)
	{
		cout << cur << " " << count << endl;
	}
	
	system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Node
{
public:
	int delicious_value;
	int caloric_value;
	Node(int caloric_value, int delicious_value) :delicious_value(delicious_value), caloric_value(caloric_value) {}

};
class myfunctor
{
public:
	bool operator()(const Node&node1, const Node&node2)
	{
		return node1.delicious_value > node2.delicious_value;
	}
};
int main()
{
	vector<Node>lunch;
	vector<Node>dinner;
	int N, M, T;
	cin >> N;//N种中饭
	cin >> M;//M种晚饭
	cin >> T;
	for (int i = 0; i < N; i++)
	{
		int caloric, delicious;
		cin >> caloric;
		cin >> delicious;
		lunch.push_back(Node(caloric, delicious));
	}
	for (int i = 0; i < M; i++)
	{
		int caloric, delicious;
		cin >> caloric;
		cin >> delicious;
		dinner.push_back(Node(caloric, delicious));
	}

	sort(lunch.begin(), lunch.end(), myfunctor());//按美味值降序排列
	sort(dinner.begin(), dinner.end(), myfunctor());
	if (T == 0)cout << 0 << endl;//特例
	else
	{
		int res = INT_MAX;
		//接下来分类讨论
		//1.中饭必吃,晚饭可吃可不吃
		for (int i = 0; i < N; i++)
		{
			if (lunch[i].delicious_value >= T)//晚饭不吃
			{
				res = min(res, lunch[i].caloric_value);
				continue;
			}
			for (int j = 0; j < M; j++)//晚饭吃
			{
				if (lunch[i].delicious_value + dinner[j].delicious_value >= T)
					res = min(res, lunch[i].caloric_value + dinner[j].caloric_value);
				else
					break;
			}
		}

		//2.晚饭必吃,中饭可吃可不吃
		for (int i = 0; i < M; i++)
		{
			if (dinner[i].delicious_value >= T)//中饭不吃
			{
				res = min(res, dinner[i].caloric_value);
				continue;
			}
			for (int j = 0; j < N; j++)//中饭吃
			{
				if (dinner[i].delicious_value + lunch[j].delicious_value >= T)
					res = min(res, dinner[i].caloric_value + lunch[j].caloric_value);
				else
					break;
			}
		}

		if (res == INT_MAX)cout << -1 << endl;
		else
			cout << res << endl;

	}


	system("pause");
	return 0;
}
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服