道具的魅力值(贪心算法)

时间限制 : 1.000 sec 内存限制 : 128 MB

题目描述

在某网络游戏中提供了一个道具库,在道具库中每种道具均有若干件(数量已知),游戏玩家购买一件道具将获得一定的魅力值。
已知每种道具的价格和魅力值,请编写一个程序,在总价格不超过某个上限的情况下使得所购道具的魅力值之和达到最大。

输入

每组测试数据的输入有n+1行,n表示道具的种类。(n<=100,p<=10000)
第1行包含两个正整数,分别表示道具种类数n和总价值的上限p,两个数字之间用空格隔开。
第2行到第n+1行分别对应于第1种道具到第n种道具的信息,每1行包含三个正整数,两个数字之间用空格隔开,三个正整数分别表示某一种道具的数量、单个道具的价格和魅力值。

输出

每组测试数据的输出只有一行,即道具魅力值的最大和。

样例输入

3 10
2 2 3
1 5 10
2 4 12

样例输出

27

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
    static class tool implements Comparable<tool> {
        int num;
        int price;
        int value;
        double rate;

        public tool(int parseInt, int parseInt1, int parseInt2, double v) {
            super();
            num=parseInt;
            price=parseInt1;
            value=parseInt2;
            rate=v;
        }

        @Override
        public int compareTo(tool o) {
            return Double.compare(o.rate,rate);
        }
    }

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        List<tool>tools=new ArrayList<tool>();
        String[] arr = sc.nextLine().split(" ");
        int n=Integer.parseInt(arr[0]);
        int c=Integer.parseInt(arr[1]);//容量
        for(int i=0;i<n;i++){
            String[] msg=sc.nextLine().split(" ");
            tools.add(new tool(Integer.parseInt(msg[0]),Integer.parseInt(msg[1]),Integer.parseInt(msg[2]),Integer.parseInt(msg[2])*1.0/Integer.parseInt(msg[1])));
        }
        Collections.sort(tools);
        int sum=0;
        for(tool t:tools){
            if(c<t.price)continue;
            int k=0;
            for(int i=1;i<=t.num;i++){
                if(i*t.price>c)break;
                else k=i;
            }
            sum+=k*t.value;
            c-=k*t.price;
        }
        // for(tool t:tools){
        //     System.out.println(t.num+" "+t.price+" "+t.value+" "+t.rate);
        // }
        System.out.println(sum);
    }
}

我的🌸🌸是个不太认路的憨憨

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是君倩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值