HDOJ1326 Box of Bricks
来源:互联网 发布:djvu转pdf软件 编辑:程序博客网 时间:2024/06/17 20:40
Box of Bricks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8281 Accepted Submission(s): 3591
Problem Description
Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. ``Look, I've built a wall!'', he tells his older sister Alice. ``Nah, you should make all stacks the same height. Then you would have a real wall.'', she retorts. After a little con- sideration, Bob sees that she is right. So he sets out to rearrange the bricks, one by one, such that all stacks are the same height afterwards. But since Bob is lazy he wants to do this with the minimum number of bricks moved. Can you help?
![](http://acm.hdu.edu.cn/data/images/1326-1.gif)
![](http://acm.hdu.edu.cn/data/images/1326-1.gif)
Input
The input consists of several data sets. Each set begins with a line containing the number n of stacks Bob has built. The next line contains n numbers, the heights hi of the n stacks. You may assume 1 <= n <= 50 and 1 <= hi <= 100.
The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.
The input is terminated by a set starting with n = 0. This set should not be processed.
The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.
The input is terminated by a set starting with n = 0. This set should not be processed.
Output
For each set, first print the number of the set, as shown in the sample output. Then print the line ``The minimum number of moves is k.'', where k is the minimum number of bricks that have to be moved in order to make all the stacks the same height.
Output a blank line after each set.
Output a blank line after each set.
Sample Input
65 2 4 1 7 50
Sample Output
Set #1The minimum number of moves is 5.
计算平均值,只算大于平均值的或者只算小于平均值的数,
例如只算小于平均值的数,用平均值减掉该数加到sum,全部加起来就是答案。
import java.util.Scanner;public class Main{private static Scanner scanner;public static void main(String[] args) {scanner = new Scanner(System.in);int cases = 1;while(scanner.hasNext()){int n = scanner.nextInt();if(n==0){break;}int arr[] = new int[n];int aver = 0;for (int i = 0; i < arr.length; i++) {arr[i] = scanner.nextInt();aver+=arr[i];}aver /= arr.length;int count = 0;for (int i = 0; i < arr.length; i++) {if(arr[i]<aver){count = count+aver-arr[i];}}System.out.println("Set #"+cases++);System.out.println("The minimum number of moves is "+count+".");System.out.println();}}}
阅读全文
0 0
- HDOJ1326 Box of Bricks
- HDOJ1326 Box of Bricks
- Box of Bricks
- 1477 Box of Bricks
- 1477:Box of Bricks
- 1477 Box of Bricks
- 591 - Box of Bricks
- HDU1326:Box of Bricks
- 591 - Box of Bricks
- 591 - Box of Bricks
- UVA591- Box of Bricks
- BNU Box of Bricks
- Box of Bricks
- hdu1326 Box of Bricks
- Box of Bricks
- Box of Bricks
- Box of Bricks
- Box of Bricks
- 互联网企业服务器资源的分配与利用
- 经典算法之基数排序两种实现
- Java equals方法特性
- mongodb centos7.3 初探
- 15算法课程 258. Add Digits
- HDOJ1326 Box of Bricks
- Qt 5 WindowFlags枚举类型
- 顺序排列的集合排序为树形集合
- golang学习之negroni/gizp源码分析
- windows安装mysql-5.7压缩版详细教程
- 二维数组输出
- windows下配置nginx+php环境
- js以逗号分隔的字符串怎么转化为数组
- 自定义ViewGroup实现流式布局