悼念512汶川大地震遇难同胞――老人是真饿了(贪心)
2016-07-10 16:26
471 查看
对于幸存的灾民来说,最急待解决的显然是温饱问题,救灾部队一边在组织人员全力打通交通,一边在组织采购粮食。现在假设下拨了一定数量的救灾经费要去市场采购大米(散装)。如果市场有m种大米,各种大米的单价和重量已知,请问,为了满足更多灾民的需求,最多能采购多少重量的大米呢?
Input
输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(0<n<=1000,0<m<=1000),分别表示经费的金额和大米的种类,然后是m行数据,每行包含2个整数p和h(1<=p<=25,1<=h<=100),分别表示单价和对应大米的重量。
Output
对于每组测试数据,请输出能够购买大米的最多重量(你可以假设经费买不光所有的大米)。
每个实例的输出占一行,保留2位小数。
Sample Input
1
7 2
3 3
4 4
Sample Output
2.33
思路:根据单价升序排列,然后贪心购买即可,注意数据类型为double
代码
Input
输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(0<n<=1000,0<m<=1000),分别表示经费的金额和大米的种类,然后是m行数据,每行包含2个整数p和h(1<=p<=25,1<=h<=100),分别表示单价和对应大米的重量。
Output
对于每组测试数据,请输出能够购买大米的最多重量(你可以假设经费买不光所有的大米)。
每个实例的输出占一行,保留2位小数。
Sample Input
1
7 2
3 3
4 4
Sample Output
2.33
思路:根据单价升序排列,然后贪心购买即可,注意数据类型为double
代码
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> #include<math.h> #include<queue> using namespace std; struct node { double price;//单价 double weight;//重量 } num[1005]; bool cmp(node x,node y) { return x.price<y.price; } int main() { int C; scanf("%d",&C); while(C--) { double N;//总经费 int M;//数据组数 scanf("%lf%d",&N,&M); for(int i=0; i<M; i++) scanf("%lf%lf",&num[i].price,&num[i].weight); sort(num,num+M,cmp); double sum_weight=0.0;//总重量 for(int i=0; i<M; i++) { if(num[i].price*num[i].weight<N) { N-=num[i].price*num[i].weight; sum_weight+=num[i].weight; } else { sum_weight+=N/num[i].price; break; } } printf("%.2lf\n",sum_weight); } return 0; }
相关文章推荐
- EFDatabase.ExecuteSqlCommand 返回值
- Android RecyclerView滑动删除和拖动排序
- 软件测试定义
- 赛场统分
- JavaScript 最佳实践
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- Android更改系统组件样式
- 多态与抽象
- HDOJ/HDU 1180 诡异的楼梯(经典BFS-详解)
- java-1.5-1.9新特性
- HDOJ/HDU 1180 诡异的楼梯(经典BFS-详解)
- Master Nginx(8) - Troubleshooting Techniques
- RSA algorithm
- 移植 FFMPEG 3.0 到 Hi3531
- 对同一个项目下的多个数据库Context进行迁移Migrations
- 接口
- maven+springmvc+dubbo的简单配置
- Moving Tables(贪心 区间覆盖)
- msgX消息发送增强工具[下载]
- 特殊权限-SUID,SGID,Sticky 学习笔记