Fork me on GitHub

牛客网编程练习之编程马拉松:砌墙

image

image

 

 

思路其实很简单,无论再少的步骤,至少要能把矮的堆到平均高度,所以只求 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();
	}

}

 

 

题目来源: https://www.nowcoder.com/practice/f1b67248244c4226a5289c993bc28128?tpId=3&tqId=10914&tPage=1&rp=&ru=/ta/hackathon&qru=/ta/hackathon/question-ranking

posted @ 2017-12-13 00:06  CC11001100  阅读(226)  评论(0编辑  收藏  举报