PAT B1020/A1070 月饼(25point(s))

题目链接
AC代码

#include<cstdio>
#include<algorithm>
using namespace std;
struct mooncake{
    double store;//库存
    double price;//单价
}cake[1010];
bool cmp(mooncake a,mooncake b){//按照单价递减排列
  return   a.price>b.price;
}

int main(){
    int  n;
    double demand,total_price,profit=0;
    scanf("%d%lf",&n,&demand);//输入月饼种类和市场需求
    for(int i=0;i<n;i++){//输入每种月饼的库存
        scanf("%lf",&cake[i].store);
    }
     for(int i=0;i<n;i++){//为每种月饼计算单价
        scanf("%lf",&total_price);
        cake[i].price=total_price/cake[i].store;
    }
    sort(cake,cake+n,cmp);//排序
    for(int i=0;i<n;i++){
        if(demand>cake[i].store){//如果目前的需求大于当前月饼库存,那么当前月饼全要了
            demand-=cake[i].store;
            profit+=cake[i].price*cake[i].store;
        }
        else{//如果当前市场需求小于当前月饼库存,只出售当前市场需求的量
            profit+=cake[i].price*demand;
            break;
        }
    }
    printf("%.2lf",profit);
    return 0;
}
发布了81 篇原创文章 · 获赞 0 · 访问量 672

猜你喜欢

转载自blog.csdn.net/weixin_44546393/article/details/105370699