思路其实很简单,无论再少的步骤,至少要能把矮的堆到平均高度,所以只求 sum(avg - (<avg))即可。
AC代码:
import java.util.Arrays; import java.util.Scanner; /** * @author CC11001100 */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()){ int n = sc.nextInt(); int[] stacks = new int[n]; for (int i = 0; i < n; i++) { stacks[i] = sc.nextInt(); } System.out.println(resolve(stacks)); } } private static int resolve(int[] n) { int avg = (int) Arrays.stream(n).average().orElse(0); return Arrays.stream(n).map(x -> x >= avg ? 0 : avg - x).sum(); } }
时间: 2024-10-11 01:55:09