Chinaunix首页 | 论坛 | 博客
  • 博客访问: 41522
  • 博文数量: 71
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 726
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-24 08:29
文章分类

全部博文(71)

文章存档

2015年(71)

我的朋友

分类: Java

2015-02-17 23:10:34

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 consideration, 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?
 

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.
 

Output

            For each set, print the minimum number of bricks that have to be moved in order to make all the stacks the same height.
Output a blank line between each set.
 

Sample Input
6
5 2 4 1 7 5
0
 

Sample Output
Set #1
The minimum number of moves is 5.
 

  1. //2.1.2
  2. import java.util.Scanner;

  3. public class Main {

  4.     public static void main(String[] args) {
  5.         int N, sum = 0;
  6.         int num = 0;
  7.         int n = 1;
  8.         int[] arr;
  9.         Scanner input = new Scanner(System.in);
  10.         while (input.hasNext()) {
  11.             N = input.nextInt();
  12.             if (N == 0) {
  13.                 break;
  14.             } else {
  15.                 arr = new int[N];
  16.                 for (int i = 0; i < N; i++) {
  17.                     arr[i] = input.nextInt();
  18.                     sum += arr[i];
  19.                 }
  20.                 int avg = sum / N;
  21.                 for (int j = 0; j < arr.length; j++) {
  22.                     if (arr[j] > avg) {
  23.                         num += arr[j] - avg;
  24.                     } else {
  25.                         num += avg - arr[j];
  26.                     }
  27.                 }
  28.                 num /= 2;
  29.             }
  30.             System.out.println("Set #" + (n++));
  31.             System.out.println("The minimum number of moves is " + num + ".");
  32.             System.out.println("");
  33.             sum = 0;
  34.             num = 0;
  35.         }
  36.     }
  37. }


阅读(128) | 评论(0) | 转发(0) |
0

上一篇:Climbing Worm

下一篇:Nasty Hacks

给主人留下些什么吧!~~