PAT 1등급 1070 Mooncake(25점)
970 단어 PAT 등급
#include
using namespace std;
struct Cake
{
float w, p, pp;
}cake[1010];
bool cmp(const Cake &c1, const Cake &c2)
{
return c1.pp>c2.pp;
}
int main(int argc, char const *argv[])
{
int n;
float d;
scanf("%d%f", &n, &d);
float w[n], p[n];
float temp;
for (int i = 0; i < n; ++i)
{
scanf("%f", &temp);
cake[i].w = temp;
}
for (int i = 0; i < n; ++i)
{
scanf("%f", &temp);
cake[i].p = temp;
cake[i].pp = temp*1.0/cake[i].w;
}
sort(cake, cake+n, cmp);
float res = 0.0;
for (int i = 0; i < n; ++i)
{
if(d-cake[i].w>=0.0)
{
res += cake[i].p;
d -= cake[i].w;
}
else
{
res += d*cake[i].pp;
break;
}
}
printf("%.2f
", res);
return 0;
}