首页 > 其他 > 详细

PAT:1020. 月饼 (25) AC

时间:2015-02-27 20:03:35      阅读:299      评论:0      收藏:0      [点我收藏+]
#include<stdio.h>
#include<algorithm>
using namespace std;

struct cake
{
  double amount;
  double sum_price,price;
}M[1010];

bool cmp(cake a,cake b)
{
  return a.price>b.price;
}

int main()
{
  double need=0;
  int kinds=0;
  scanf("%d%lf",&kinds,&need);
  for(int i=0 ; i<kinds ; ++i)
  {
    scanf("%lf",&M[i].amount);
  }
  for(int i=0 ; i<kinds ; ++i)
  {
    scanf("%lf",&M[i].sum_price);
    M[i].price=M[i].sum_price/M[i].amount;    //存储单价
  }
  sort(M,M+kinds,cmp);              //按照单价从大到小排列

  double ans=0;
  for(int i=0 ; i<kinds ; ++i)      //中间“need>=0”改为遍历i结束
  {
    if(need<=M[i].amount)
    {
      ans+=need*M[i].price;    //这次够了,加完就结束
      break;
    }
    else if(need>M[i].amount)
    {
      ans+=M[i].sum_price;
      need-=M[i].amount;
    }
  }
  printf("%.2f\n",ans);
  return 0;
}

PAT:1020. 月饼 (25) AC

原文:http://www.cnblogs.com/Evence/p/4304293.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!