pat每日刷题计划--day68

PAT B1020 月饼

题目是典型贪心,注意题目描述正数,可能是double,不要想当然或者只看样例

另外注意 double的读取是%lf,输出是%f,如果两位小数%.2f

%lld是对longlong的(longlong的输入输出是%lld或者%l64d,输入输出同)

#include
#include
#include<string.h>
#include
using namespace std;
struct yuebing
{
    double kucun;
    double shoujia;
    double danjia;
}yb[1005];
bool cmp(yuebing a,yuebing b)
{
    return a.danjia>b.danjia;
}
int main()
{
    int num=0,sum=0;
    double money=0;
    scanf("%d %d",&num,&sum);
    for(int i=0;i<=num-1;i++)
        scanf("%lf",&yb[i].kucun);
    for(int i=0;i<=num-1;i++)
    {
        scanf("%lf",&yb[i].shoujia);
        yb[i].danjia=yb[i].shoujia*1.0/yb[i].kucun;
    }
    sort(yb,yb+num,cmp);
    int temp=0;
    while(sum>0 && temp<num)
    {
        //cout<
        if(yb[temp].kucun<=sum)
        {
            sum=sum-yb[temp].kucun;
            money+=yb[temp].shoujia;
            temp++;
        }
        else
        {
            money+=sum*yb[temp].danjia;
            sum=0;
            temp++;
        }
    }
    printf("%.2f",money);
    return 0;
}
View Code

你可能感兴趣的:(pat每日刷题计划--day68)