pat1020:月饼

https://www.patest.cn/contests/pat-b-practise/1020

#include "stdio.h"

struct yb{
	float stock;
	float price;
}yb[1000];
void swap(struct yb *a, struct yb *b)
{
	struct yb temp;
	temp = *a;
	*a = *b;
	*b = temp;
}
int main()
{
	int i, j, n, d;
	float sum = 0;
	scanf("%d %d", &n, &d);
	for(i = 0; i < n; ++i)
	{
		scanf("%f", &yb[i].stock);
		scanf("%f", &yb[i].price);
	}
	for(i = 0; i < n; ++i)
		for(j = i+1; j < n; ++j)
			if(yb[i].price / yb[i].stock < yb[j].price / yb[j].stock)
				swap(&yb[i], &yb[j]);

	for(i = 0; i < n; ++i)
		if(yb[i].stock < d)
		{
			d -= (int)yb[i].stock;
			sum += yb[i].price;
		}
		else
		{
			sum = sum + (float)(yb[i].price * (1.0 * d)/yb[i].stock);
			break;
		}
	printf("%.2f\n", sum);

	return 0;
}


你可能感兴趣的:(c,pat)