A1070/B1020 Mooncake (25 分)【避坑!

题目给的输入条件为如下,尤其注意用黑体字表明的地方。

Each input file contains one test case. For each case, the first line contains 2 positive integers N (≤1000), the number of different kinds of mooncakes, and D (≤500 thousand tons), the maximum total demand of the market. Then the second line gives the positive inventory amounts (in thousand tons), and the third line gives the positive prices (in billion yuans) of N kinds of mooncakes. All the numbers in a line are separated by a space.

  • 对于N和D的介绍,题目用的是 positive integers 【正整数】
  • 对于库存和价格的介绍,题目用的是 positive integerspositive prices 【正库存和正价格】,而没有说是正整数!!! 所以,一定要用double型存储。
#include <bits/stdc++.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

struct Mooncake{
    
    
	double kg;
	double price;
}cake[1010];

bool cmp(Mooncake a, Mooncake b){
    
    
	return (a.price / a.kg ) > (b.price / b.kg); 
}

int main(int argc, char** argv) {
    
    
	
	int n, d;
	cin >> n >> d;
	
	double profit = 0.0;
	
	for(int i = 0; i < n; i++){
    
    
		cin >> cake[i].kg;
	}
	for(int i = 0; i < n; i++){
    
    
		cin >> cake[i].price;
	}
	
	sort(cake, cake+n, cmp);
	
	for(int i = 0; i < n; i++){
    
    
		if(cake[i].kg <= d){
    
    
			d -= cake[i].kg;
			profit += cake[i].price;
		}else {
    
    
			profit += (cake[i].price / cake[i].kg) * d;
			break;
		}
	}
	printf("%.2lf", profit);
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/alovelypeach/article/details/114404049