L2-003 Mooncake (25 points)

Mooncake is a traditional food that Chinese people eat during the Mid-Autumn Festival, and there are many mooncakes with different flavors in different regions. Given the inventory of all types of mooncakes, the total selling price, and the maximum demand in the market, please calculate the maximum profit you can get.

Note: Part of the stock is allowed to be withdrawn at the time of sale. The situation given in the example is as follows: if we have 3 kinds of moon cakes, the inventory is 180,000 tons, 150,000 tons, and the total selling price is 7.5 billion, 7.2 billion, and 4.5 billion yuan. If the maximum demand in the market is only 200,000 tons, then our maximum profit strategy should be to sell all 150,000 tons of the second type of moon cakes and 50,000 tons of the third type of moon cakes to obtain 72 + 45/2 = 9.45 billion (billion yuan) .

Input format:
Each input contains one test case. Each test case first gives a positive integer N not exceeding 1000 to represent the number of moon cake types, and a positive integer D not exceeding 500 (in 10,000 tons) representing the maximum market demand. The next line gives N positive numbers to indicate the inventory of each kind of moon cake (in 10,000 tons); the last line gives N positive numbers to indicate the total selling price of each kind of moon cake (in 100 million yuan). The numbers are separated by spaces.

Output format:
For each group of test cases, output the maximum profit in one line, in billions of yuan and accurate to 2 decimal places.

Input sample:
3 20
18 15 10
75 72 45
Output sample:
94.50

在这里插入代码片

#include<stdio.h>
int main()
{
    
    
	int i, j, n, m;
	scanf("%d %d", &n, &m);
	
	
	double a[n], b[n];//数据类型必须全是double要不然有一个测试点过不去 
	for(i=0; i<n; i++) {
    
    
		scanf("%lf", &a[i]);
	}
	
	for(i=0; i<n; i++) {
    
    
		scanf("%lf", &b[i]);
	}
	
//	求月饼平均价格 
	double c[n];
	for(i=0; i<n; i++) {
    
    
		c[i] = b[i] / a[i];
	}
	
	
//	根据价格排序 
	for(i=0; i<n; i++) {
    
    
		int index=i;
		for(j=i+1; j<n; j++) {
    
    
			if(c[index] < c[j]) index = j;
		}
		double t = c[i];
		c[i] = c[index];
		c[index] = t;
		
		int q = a[i];
		a[i] = a[index];
		a[index] = q;
		
		int w = b[i];
		b[i] = b[index];
		b[index] = w;
	}
	
	double sum = 0;
	for(i=0; i<n; i++) {
    
    
		if(m > a[i]) {
    
    
			sum = sum + b[i];
			m = m - a[i];
		}
		else {
    
    
			sum = sum + c[i] * m;
			m = 0;
			break;
		}
	}
	
	printf("%.2f", sum);
	
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324033925&siteId=291194637