HDU-1009,FatMouse' Trade(贪心水题)

简介: HDU-1009,FatMouse' Trade(贪心水题)

Problem Description:


FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.

The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.


Input:


The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.


Output:


For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.


Sample Input:


5 3


7 2


4 3


5 2


20 3


25 18


24 15


15 10


-1 -1


Sample Output:


13.333


31.500


程序代码:


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
  double a,b,c;
}x[1001];
int cmp(node a,node b)
{
  return a.c>b.c;//返回较大的那个值 
}
int main()
{
  int m,n,i;
  double j,f,sum;
  while(scanf("%d %d",&m,&n)&&(m!=-1&&n!=-1))
  {
    sum=0;
    for(i=0;i<n;i++)
    {
      scanf("%lf %lf",&j,&f);
      x[i].a=j;
      x[i].b=f;
      x[i].c=j/f;
    }
    sort(x,x+n,cmp);//从大到小排序 
    for(int k=0;k<n;k++)
    {
      if(m>=x[k].b)//当m大于整体的b时 
      {
        sum+=x[k].a;
        m-=x[k].b;
      }
      else//当m不能整体交换时 
      {
        sum+=m*x[k].a/x[k].b;
        break;
      }
    }
    printf("%.3f\n",sum);
  }
  return 0;
}


相关文章
|
JSON 小程序 算法
uniapp小程序封装常用工具函数
类似的工具函数还有很多,这里暂时不一一列举了,上面提到的银行卡校验,还可以使用阿里公开的一个校验接口cardNo=银行卡号码&cardBinCheck=true这个接口会返回对应的银行简称银行卡类型,如果想要显示对应的银行logo的话,可以联系我,由于JSON数据太大,没办法全部放在这里。
368 0
计算机组成原理(微课版) -- 第三章 -- 运算方法与运算器
计算机组成原理(微课版) -- 第三章 -- 运算方法与运算器
|
6月前
|
人工智能 编解码 芯片
告别低效沟通|让技术提问不再头疼-这套高效AI提问模板来帮你
不会向ai提问,不知道怎么提问的 可以看看
5871 1
告别低效沟通|让技术提问不再头疼-这套高效AI提问模板来帮你
|
12月前
|
存储 Java
抽象类与抽象方法
抽象类和抽象方法是面向对象编程中的重要概念。抽象类不能被实例化,主要用于定义子类的通用接口或行为。抽象方法是在抽象类中声明但未实现的方法,要求子类必须实现这些方法。这种方式有助于构建灵活且可扩展的代码结构。
319 2
|
12月前
|
边缘计算 人工智能 搜索推荐
移动应用与系统:技术演进与未来展望
【10月更文挑战第39天】在数字时代的浪潮中,移动应用和操作系统作为连接用户与数字世界的桥梁,其技术的演进不仅改变了我们的生活方式,还不断推动着社会的数字化转型。本文将探讨移动应用开发的最新趋势、移动操作系统的技术革新,以及这些变化如何塑造我们的未来。通过深入浅出的分析,我们将一窥移动技术的未来蓝图,并思考如何在不断变化的技术环境中保持竞争力。
145 0
|
Oracle 关系型数据库 Windows
如何彻底卸载 IDEA,将IDEA完全删除
本文提供了一个详细的教程,指导用户如何彻底卸载 IntelliJ IDEA,包括使用Uninstall.exe程序和通过注册表删除残留项的步骤。
7307 3
如何彻底卸载 IDEA,将IDEA完全删除
|
存储 缓存 算法框架/工具
Transformers 4.37 中文文档(十三)(5)
Transformers 4.37 中文文档(十三)
195 1
|
传感器 流计算
Flink(十一)【状态管理】(2)
Flink(十一)【状态管理】
【2022天梯赛】L1-7 机工士姆斯塔迪奥
【2022天梯赛】L1-7 机工士姆斯塔迪奥
130 0
|
Ubuntu NoSQL Linux
linux0.11+Bochs环境搭建和使用
linux0.11+Bochs环境搭建和使用
下一篇
开通oss服务