文章

33

粉丝

42

获赞

3

访问

7.6k

头像
喝饮料 题解:(60%)
P1478 云南大学机试题
发布于2024年3月17日 11:28
阅读数 219

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
struct goods{
	double mi;
	double wi;
};
bool ascd(goods a,goods b){

	  return a.wi/a.mi < b.wi/b.mi ;
}
int main(){
	int x,n;
	double total = 0;
	goods g[1000];
	while(cin >>x >> n){
		if(x == -1 && n== -1)
		   return 0;
		for(int i = 0;i < n;i++){
			cin >>g[i].mi >>g[i].wi; 
		}
		stable_sort(g,g + n,ascd);
		int j = 0;
		while(x >0&&j <n){
			if(g[j].wi <= x){
				total += g[j].mi;
				x -=g[j].wi;
				j++;
			}
			else{
				total += x*(g[j].mi/g[j].wi);
				x =0;
			}
       }
       printf("%.3lf\n",total);
	}
	return 0;
}

 

登录查看完整内容


登录后发布评论

1 条评论
snake
2024年3月17日 15:06

total初始化要放在循环里,否则多组数组会出错影响到下一组数据的计算

赞(0)