2020年3月3日 PAT

A1020 25’ 贪心 easy

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct cake{
	double store;
	double sell;
	double price;
};
bool cmp(cake a, cake b)
{
	return a.price > b.price;
}
vector<cake> cakes;
void p(vector<cake> cakes)
{
	for (auto i : cakes)
	{
		printf("%f %f %f\n", i.store, i.sell, i.price);
	}
}
void test()
{
	int n;
	double m;
	cin >> n >> m;
	cake a;
	while (n--)
	{
		cin >> a.store;
		cakes.push_back(a);
	}
	for (int i = 0; i < cakes.size(); ++i)
	{
		cin >> a.sell;
		cakes[i].sell = a.sell;
		cakes[i].price = cakes[i].sell / cakes[i].store;
	}
	sort(cakes.begin(), cakes.end(),cmp);
	double sum = 0;
	for (auto i : cakes)
	{
		if (i.store <= m)
		{
			sum += i.sell;
			m -= i.store;
		}
		else
		{
			sum = sum + i.price*m;
			break;
		}
	}
	printf("%.2lf", sum);
}
int main()
{
	test();
	system("pause");
	return 0;
}

B1023 20’ 贪心 easy

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

void test()
{
	int a[10];
	for (int i = 0; i < 10; ++i)
		scanf_s("%d", &a[i]);
	for (int i = 1; i < 10; ++i)
	{
		if (a[i] > 0)
		{
			cout << i;
			a[i]--;
			break;
		}
	}
	for (int i = 0; i < 10; ++i)
	{
		for (int j = 0; j < a[i]; ++j)
		{
			cout << i;
		}
	}
}
int main()
{
	test();
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值