PAT (Advanced Level) 1070. Mooncake (25)

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT (Advanced Level) 1070. Mooncake (25)相关的知识,希望对你有一定的参考价值。

简单贪心。先买性价比高的。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;

double eps=1e-7;
const int maxn=1000+10;
int n;
struct X
{
    double x;
    double y;
}s[maxn];
double money;

bool cmp(const X&a,const X&b)
{
    return a.y/a.x>b.y/b.x;
}

int main()
{
    scanf("%d",&n);
    scanf("%lf",&money);
    for(int i=1;i<=n;i++) scanf("%lf",&s[i].x);
    for(int i=1;i<=n;i++) scanf("%lf",&s[i].y);
    sort(s+1,s+1+n,cmp);
    double ans=0;
    for(int i=1;i<=n;i++)
    {
        if(money>=s[i].x)
        {
            ans=ans+s[i].y;
            money=money-s[i].x;
        }
        else
        {
            ans=ans+money*s[i].y/s[i].x;
            break;
        }
    }
    printf("%.2lf\n",ans);
    return 0;
}

 

以上是关于PAT (Advanced Level) 1070. Mooncake (25)的主要内容,如果未能解决你的问题,请参考以下文章

PAT Advanced Level 1044

PAT Advanced Level 1043

PAT Advanced Level 1079

PAT Advanced Level 1095

PAT Advanced Level 1038

pat advanced level 1063