码迷,mamicode.com
首页 > 编程语言 > 详细

PAT甲题题解-1070. Mooncake (25)-排序,大水题

时间:2017-02-12 12:42:14      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:class   nts   div   mount   name   close   技术   block   blocks   

技术分享
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>

using namespace std;
/*
3 0
180 150 100
7.5 7.2 4.5
*/
const int maxn=1000+5;
int amounts[maxn];
float price[maxn];

struct Cake{
    float amounts;  //注意,这里得设置成浮点型,若是int一个样例会过不了
    float price;
    float unitprice=0;
    bool operator<(const Cake tmp)const{
        return unitprice>tmp.unitprice;
    }
}cake[maxn];
int main()
{
    int n,d;
    scanf("%d %d",&n,&d);
    for(int i=0;i<n;i++){
        scanf("%f",&cake[i].amounts);
    }
    for(int i=0;i<n;i++){
        scanf("%f",&cake[i].price);
        cake[i].unitprice=cake[i].price/cake[i].amounts;
    }
    sort(cake,cake+n);
    float ans=0;
    for(int i=0;i<n;i++){
        if(d>=cake[i].amounts){
            ans+=cake[i].price;
            d-=cake[i].amounts;
        }
        else{
            ans+=(d/cake[i].amounts)*cake[i].price;
            break;
        }
    }
    printf("%.2f",ans);
    return 0;
}
View Code

 

PAT甲题题解-1070. Mooncake (25)-排序,大水题

标签:class   nts   div   mount   name   close   技术   block   blocks   

原文地址:http://www.cnblogs.com/chenxiwenruo/p/6390694.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!