PAT1 1070 Mooncake

题目链接
我的github

题目大意

给一些不同种类的月饼的数量和总价格,市场需求量是 D D D,求卖出月饼所能得到的最大收益

输入

每组包含一个测试用例

  • 用例第一行是两个正整数 N ≤ 1000 N\le1000 N1000表示月饼的种类数量, D ≤ 500 D\le500 D500表示市场需求量
  • 第二行有 N N N个正数,表示 N N N种月饼的数量
  • 第三行有 N N N个正数,表示 N N N种月饼的总价格

输出

对每个样例输出卖得的最大收益

样例输入

3 200
180 150 100
7.5 7.2 4.5

样例输出

9.45

解析

要卖出的收益最大,就先求出每种月饼的单价,然后依次从最贵的开始卖即可

我写的python有个测试点出现非0返回
然后又参考了dalao:传送门

我的python:

# -*- coding: utf-8 -*- 
# @Time : 2019/6/28 14:36 
# @Author : ValarMorghulis 
# @File : 1070.py
import functools


class moonCake:
    def __init__(self, amounts, tot):
        self.amounts = amounts
        self.tot = tot
        self.price = tot / amounts


def cmp(a, b):
    return -1 if a.price < b.price else 1


def solve():
    n, d = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(float, input().split()))
    cake = list()
    for i in range(n):
        cake.append(moonCake(a[i], b[i]))
    cake = sorted(cake, key=functools.cmp_to_key(cmp), reverse=True)
    ans = 0
    for i in range(n):
        if cake[i].amounts < d:
            ans += cake[i].tot
            d -= cake[i].amounts
        else:
            ans = ans + cake[i].price * d
            break
    print("%.2f" % ans)


if __name__ == "__main__":
    solve()

dalao的python:

if __name__ == "__main__":
    line = input().split(" ")
    n, total = int(line[0]),int(line[1])
    line = input().split(" ")
    support = [float(x) for x in line]
    line = input().split(" ")
    price = [float(x) for x in line]
    per = []
    for x in range(n):
        if support[x] != 0 and price[x] != 0:
            unit = [price[x]/support[x], support[x], price[x]]
            per.append(unit)
    per = sorted(per, key = lambda x : -x[0])
    money = 0
    while(total > 0 and len(per) > 0):
        unit = per.pop(0)
        if total >= unit[1]:
            money += unit[2]
            total -= unit[1]
        else:
            money += total * unit[0]
            total = 0
    print('{:.2f}'.format(money))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main{ public static void main(String[] args) throws IOException { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); int n; double need; double sum=0; String s[]=in.readLine().split(" "); n=Integer.parseInt(s[0]); need=Double.parseDouble(s[1]); String amount[]=in.readLine().split(" "); String price[]=in.readLine().split(" "); ArrayList<Mooncake> cakes=new ArrayList<Mooncake>(); for(int i=0;i<n;i++){ Mooncake cake=new Mooncake(); cake.setAmount(Double.parseDouble(amount[i])); cake.setSales(Double.parseDouble(price[i])); cake.setValues(Double.parseDouble(price[i])/Double.parseDouble(amount[i])); cakes.add(cake); } Collections.sort(cakes); for(Mooncake c: cakes){ if(need>=c.amount){ need=need-c.amount; sum+=c.amount*c.values; } else{ sum+=need*c.values; need=0; } if(need==0){ break; } } System.out.printf("%.2f",sum); } static class Mooncake implements Comparable<Mooncake>{ double amount; double sales; double values; public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } public double getSales() { return sales; } public void setSales(double sales) { this.sales = sales; } public double getValues() { return values; } public void setValues(double values) { this.values = values; } @Override public int compareTo(Mooncake arg0) { // TODO Auto-generated method stub return this.values>arg0.values?-1:1; } } }转c++
05-23

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值