题解 | #数的选择#
数的选择
https://www.nowcoder.com/practice/12f72e06fc424e4d9c685cd89f2bed36
import java.util.Arrays; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long num = Long.parseLong(scanner.next()); long[] list = new long[(int) (num+1)]; for (int i = 1; i <= num; i++) { list[i] = Long.parseLong(scanner.next()); } Arrays.sort(list); long[] preSum = new long[(int) (num+2)]; for (int i = (int) num; i >= 1; i--) { preSum[i] = preSum[i+1] + list[i]; } boolean ifMatch = true; for (int i = (int) num; i >= 1; i--) { if (num+3-i <= num + 1) { if (preSum[i] > preSum[1] - preSum[(int) (num+3-i)]) { System.out.println(num - i + 1); ifMatch = false; break; } } } if (ifMatch) System.out.println("-1"); } }