为了更深层次的了解java,继续上OJ A题
import java.io.BufferedInputStream; import java.util.Arrays; import java.util.Scanner; public class Main { private static Scanner cin = new Scanner(new BufferedInputStream(System.in)); private static int n, m; private static Node[] node; public static void main(String[] args) { double sum; while (true) { n = cin.nextInt(); m = cin.nextInt(); if (n + m == -2) { break; } sum = 0; node = new Node[m]; for (int i = 0; i < m; i++) { node[i] = new Node(cin.nextInt(), cin.nextInt()); } Arrays.sort(node); for (int i = 0; i < m && n > 0; i++) { if (n > node[i].y) { n -= node[i].y; sum += node[i].x; } else { sum += n * 1.0 / node[i].y * node[i].x; n = 0; } } System.out.printf("%.3f", sum);//直接加\n竟然PE了, 需要\r\n或者加上下面一行 System.out.println(); } } } class Node implements Comparable<Node>{ int x, y; double p; public Node(int x, int y) { this.x = x; this.y = y; setP(); } void setP(){ p = x * 1.0 / y; } @Override public int compareTo(Node node) { return (p < node.p)? 1:-1; } }