悼念512汶川大地震遇难同胞——老人是真饿了(杭电OJ2187)解题报告

本文通过一个具体的水提问题实例,介绍了如何运用基础的贪心算法进行求解。该实例涉及了输入处理、数据结构定义、比较函数实现及快速排序等关键步骤,并详细展示了算法的执行流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

水提,不讲太多,就是最基础的贪心算法

代码如下:

Code:
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<iomanip>
  4. usingnamespacestd;
  5. typedefstructNode
  6. {
  7. intp;
  8. inth;
  9. }data;
  10. intComp(constvoid*p1,constvoid*p2)
  11. {
  12. return((Node*)p2)->p<((Node*)p1)->p?1:-1;
  13. }
  14. intmain()
  15. {
  16. intc;
  17. datarice[1000];
  18. cin>>c;
  19. while(c)
  20. {
  21. floatbuy=0;
  22. intn,m;
  23. cin>>n>>m;
  24. for(inti=0;i<m;i++)
  25. cin>>rice[i].p>>rice[i].h;
  26. qsort(rice,m,sizeof(data),Comp);
  27. //for(i=0;i<m;i++)
  28. //cout<<rice[i].p<<""<<rice[i].h<<endl;
  29. for(i=0;i<m;i++)
  30. {
  31. if(n<rice[i].h*rice[i].p)
  32. {
  33. buy+=(float)n/rice[i].p;
  34. break;
  35. }
  36. else
  37. {
  38. buy+=rice[i].h;
  39. n=n-rice[i].h*rice[i].p;
  40. }
  41. }
  42. printf("%.2f/n",buy);
  43. c--;
  44. }
  45. return0;
  46. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值