第二点WA,实在想不出怎么改,请大佬们帮忙
查看原帖
第二点WA,实在想不出怎么改,请大佬们帮忙
308026
invokerKing楼主2020/6/12 23:05
import java.util.TreeSet;


class Main {
	static class Gold implements Comparable{
		double m;
		double v;
		double pc;
		public Gold(double m,double v){
			this.m=m;
			this.v=v;
			pc=v/m;
		}
		//按照pc从大到小排列
		@Override
		public int compareTo(Object o) {
			if (o instanceof Gold) {
				Gold gold=(Gold)o;
				return -(Double.compare(this.pc, gold.pc));
			}
			else {
				throw new RuntimeException("");
			}
		}
		
	}
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		int N=input.nextInt();
		int T=input.nextInt();
		TreeSet<Gold> set=new TreeSet<Gold>();
		for (int i = 0; i <N; i++) {
			int m=input.nextInt();
			int v=input.nextInt();
			set.add(new Gold(m,v));
		}
		double sum=0;
		for (Gold gold : set) {
			if (T>gold.m) {
				sum+=gold.v;
				T-=gold.m;
			}
			else {
				sum+=T*gold.pc;
				break;
			}
		}
		System.out.println(String.format("%.2f", sum));
	}
}
2020/6/12 23:05
加载中...