PAT:1070. Mooncake (25) AC

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

struct moomcake
{
  double store;      //【cation】这个可能是小数!
  double sumPrice;
  double solePrice;
}M[1010];

bool cmp(moomcake a,moomcake b)
{
  return a.solePrice>b.solePrice;
}

int main()
{
  memset(M,-1,sizeof(M));
  int n;
  double d;
  scanf("%d%lf",&n,&d);
  for(int i=0 ; i<n ; ++i)
    scanf("%lf",&M[i].store);
  for(int i=0 ; i<n ; ++i)
  {
    scanf("%lf",&M[i].sumPrice);
    M[i].solePrice=M[i].sumPrice/M[i].store;
  }
  sort(M,M+n,cmp);
  double p=0;
  for(int i=0 ; i<n && d>0; ++i)
  {
    if(M[i].store<=d)
    {
      p+=M[i].sumPrice;
      d-=M[i].store;
    }
    else if(M[i].store>d)
    {
      p+=M[i].solePrice*d;
      break;
    }
  }
  printf("%.2f
",p);
  return 0;
}
原文地址:https://www.cnblogs.com/Evence/p/4325704.html