时间限制 : 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);
}
}
我的🌸🌸是个不太认路的憨憨