1070. Mooncake 解析

对单价排序 然后一个个算就好啦

#include 
#include 
#include 

#define MAX 1010

using namespace std;
int n, ton;

struct Node{
	float ton;
	float prise;
	float avg;
};
Node MoonCake[MAX];

bool cmp(Node n1, Node n2) {
	return n1.avg > n2.avg;
}

int main() {
	cin >> n >> ton;

	for(int i = 0; i < n; i++) {
		cin >> MoonCake[i].ton;
	}

	for (int i = 0; i < n; i++) {
		cin >> MoonCake[i].prise;
		MoonCake[i].avg =MoonCake[i].prise / MoonCake[i].ton;
	}

	sort(&MoonCake[0], &MoonCake[0] + n, cmp);

	int pos = 0;
	float profit = 0;
	while (ton && pos < n) {
		float left = ton - MoonCake[pos].ton;
		if (left < 0){
			profit += ton * MoonCake[pos].prise / MoonCake[pos].ton;
			break;
		}
		else {
			profit +=  MoonCake[pos].prise;
			ton = left;
			pos++;
		}
	}

	printf("%.2f\n", profit);

	return 0;


}


你可能感兴趣的:(PAT)