标签:
这道题比较简单,但是一直MLE.
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); short M=0,N=0,j,f; Food[] fs; while(true) { M=scanner.nextShort(); N=scanner.nextShort(); if(N==-1 && M==-1) break; fs=new Food[N]; for(short i=0;i<N;i++) { fs[i]=new Food(scanner.nextShort(),scanner.nextShort()); } Arrays.sort(fs);//注意这里的N和String的subString的endIndex一样都不包含在内 float sum=0; short i=0; while(M>0 && i<N) { if(fs[i].f>=M) { sum+=M*fs[i].j*1.0f/fs[i].f; M=(short)(M-M); } else { sum+=fs[i].j; M=(short)(M-fs[i].f); } i++; } System.out.println(String.format("%.3f", sum)); } } static class Food implements Comparable<Food> { public short j; public short f; public Food(short j,short f) { this.j=j; this.f=f; } @Override public int compareTo(Food o) { // TODO Auto-generated method stub if(this.j*1.0f/this.f>=o.j*1.0f/o.f) return -1; else return 1; } } }
标签:
原文地址:http://www.cnblogs.com/maydow/p/4601042.html