1020 月饼 (25 分)

2019-04-30  本文已影响0人  79d12e22ec53

#include <stdio.h>

#include <stdlib.h>

#define MAXN 1001



typedef struct {

    float stock;    

    float price;   

    float each;  

} mooncake;

int cmp ( const void *a, const void*b ) {

    mooncake c1 = *(mooncake *)a;  

    mooncake c2 = *(mooncake *)b;

    return c2.each > c1.each;   

}

int main(void) {

    int N, i;

    float D, total;

    mooncake cake[MAXN];

    

    scanf("%d %f", &N, &D);

    for ( i = 0; i < N; i++ )   scanf("%f", &cake[i].stock);    

    for ( i = 0; i < N; i++ )   scanf("%f", &cake[i].price);

    for ( i = 0; i < N; i++ )   cake[i].each = cake[i].price / cake[i].stock;



    qsort ( cake, N, sizeof(mooncake), cmp );    

    total = 0;



    for( i = 0; i < N && D > 0; i++ ) {

        if ( cake[i].stock <= D ) {

            total += cake[i].price;

            D -= cake[i].stock;    

        } else {  

            total += D * cake[i].each; 

            D = 0;  

        }

    }

    printf("%.2f\n", total);

    

    return 0;

}

上一篇 下一篇

猜你喜欢

热点阅读