Box of Bricks JAVA

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.

输入由几个数据集组成。每个集合都以一行开始,其中包含 Bob 构建的栈数 n。下一行包含 n 个数字,n 堆栈的高度 hi。可以假设1≤ n ≤50,1≤ hi ≤100。
砖块的总数可以用堆栈的数量来整除。因此,总是可以重新排列砖块,使所有的堆栈具有相同的高度。
输入被一个以 n = 0开始的集合终止。这个集合不应该被处理。

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.

题目解读

  • 要求输入一个n,n表示一个多少个柱子stack。

  • 接着输入每个柱子的砖数(高度),用数组表示此集合。

  • 0根柱子的时候退出程序,不作处理。

  • 示例:
    2
    1 5
    表示有2个柱子,高度分别是1 5。

  • 题目设定高度总和能被n整除,即输入的高度数据之和默认为n的整倍。

  • 要求输出集合的序号Set和最少移动砖数moves。

  • 集合的序号Set可用一个count递增,初始化为1。

  • 最少移动砖数moves=(每条大于平均高度的 - 平均高度) 的总和
    用上面简单的例子:
    2
    1 5
    只需要把5上面的2块砖搬到1就可以一样高了(都为3), moves=2.
    平均高度为(1+5)/2=3
    大于平均高度的有5,5-3=2.

/**
 * 移动最小砖块使所有柱子一样平
 */
import java.util.Scanner;
public class Move {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int count=1;
        int sum,n,average,moves;//总砖数,柱子数,平均高度,移动数
        int[] array=new int[50];
        while(true){
            n=s.nextInt();//n指某个集合中有n个柱子
            if(n==0) break;
            sum=0;
            for(int i=0;i<n;i++){
                array[i]=s.nextInt();//输入n个柱子的高度
                sum+=array[i];
            }
            average=sum/n;
            moves=0;
            for(int i=0;i<n;i++){
                if(array[i]>average){
                    moves+=array[i]-average;//计算最少移动
                }
            }
            System.out.println("Set #"+count);
            System.out.println("The minimum number of moves is "+moves+".");
            System.out.println();
            count++;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值