pdd笔试题

08/05 15:32
阅读数 68

拼多多提前批的笔试没有报名,但昨天听伙伴们说很难,所以一共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;
}
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部