【PAT甲级】1070. Mooncake (25)

来源:互联网 发布:函数式编程语言 编辑:程序博客网 时间:2024/06/10 13:30
#include <stdio.h>#include <algorithm>using namespace std;typedef struct Cake {    double ton;    double price;    double up;} Cake;bool cmp(Cake a, Cake b) {    return a.up > b.up;}int main(int argc, char *argv[]) {    int n;    double t;    scanf("%d %lf", &n, &t);    Cake *c = new Cake[n];    for (int i = 0; i < n; i++) {        scanf("%lf", &c[i].ton);    }    for (int i = 0; i < n; i++) {        scanf("%lf", &c[i].price);        c[i].up = c[i].price / c[i].ton;    }    sort(c, c + n, cmp);    int i = 0;    double p = 0.0;    while (t > 0 && i < n) {        if (c[i].ton > t) {            p += t * c[i].up;            break;        } else {            p += c[i].price;            t -= c[i].ton;            i++;        }    }    printf("%.2lf\n", p);    return 0;}
原创粉丝点击