PAT:1020. 月饼 (25) AC

#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;
}
posted on 2015-02-27 19:47  Evence  阅读(676)  评论(0编辑  收藏  举报