# java之c语言struct用法

4 15
100 4
412 8
266 7
591 2

1193.0

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {

public static int n,C;
public static Struct struct[];

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
n = in.nextInt();
C = in.nextInt();
struct = new Struct[n];
for(int i=0;i<n;i++) {
int value = in.nextInt();
int weight = in.nextInt();
struct[i] = new Struct(value, weight, value*(1.0)/weight);
}
Comparator<Struct>comparator = new Mycomparator();

//sort排序方法的第二种用法,两个参数，重载compare函数，设置自己的比较方法
Arrays.sort(struct,comparator);
double totv = 0,totw = 0;
for(int i=0;i<n;i++) {    //按单位重量的价值从大到小一次放入
if(totw + struct[i].weight <= C) {   //未放满
totv += struct[i].value;
totw += struct[i].weight;
}else {
totv += struct[i].desity * (C-totw);
totw = C;
break;
}
}
System.out.printf("%.1f",totv);
}
}

//自定义的排序方法
class Mycomparator implements Comparator<Struct>
{

public int compare(Struct o1, Struct o2) {   //降序

if(o1.desity<o2.desity)
return 1;
else if(o1.desity==o2.desity)
return 0;
else
return -1;
}

}

public class Struct {

int value;
int weight;

double desity;

public Struct(int value,int weight,double desity){

this.value = value;

this.weight = weight;

this.desity = desity;

}

}