解题思路:
注意事项:
参考代码:
import java.awt.print.Book; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int m = sc.nextInt();//接收几组数据 for (int i = 0; i < m; i++) { int n = sc.nextInt();//小朋友数量 int[][] arr = new int[n][2];//小朋友有的积木数和他一共需要的积木数 for (int j = 0; j < n; j++) { arr[j][0] = sc.nextInt(); arr[j][1] = sc.nextInt(); } Arrays.sort(arr,new Comparator<int[]>(){ public int compare(int[]o1,int[]o2) { return (o1[1]-o1[0])-(o2[1]-o2[0])>0?1:-1; } }); int num = 0; boolean f = true;//假设玩的下去 for (int j = 0; j < n; j++) { //当前小朋友已有的积木数量+积木池的数量>需要的数量 if (arr[j][0]+num>=arr[j][1]) { num+=arr[j][0]; } else { f = false; break; } } if (f == true) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
0.0分
5 人评分