HDOJ 1326 Box of Bricks(简单题)

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?

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, 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.

Sample Input
6
5 2 4 1 7 5
0

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

题目很简单:就是先输入一个数n,然后再接n个数,
如果n为0,就结束输入。
然后求n个数的平均数,再求出这n个数中(比平均数大的数一共比平均数大多少的和)比平均数小的数一共比平均数小多少的和。
这个和就是要求的。

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int tp=1;
        while(sc.hasNext()){
            int n = sc.nextInt();
            if(n==0){
                return ;
            }
            int[] num = new int[n];
            int sum=0;
            for(int i=0;i<n;i++){
                num[i] = sc.nextInt();
                sum+=num[i];
            }

            int s = sum/n;

            int times = 0;
            for(int i=0;i<n;i++){
                if(num[i]<s){
                    times+=s-num[i];
                }
            }
            System.out.println("Set #"+(tp++));
            System.out.println("The minimum number of moves is "+times+".");
            System.out.println();
        }

    }

}
时间: 2024-10-13 19:55:10

HDOJ 1326 Box of Bricks(简单题)的相关文章

算法题:UVa 591 Box of Bricks (模拟)

591 - Box of Bricks Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=53 2 Little Bob likes playing with his box of bricks. He puts the bricks one upon an

HDOJ(HDU) 2088 Box of Bricks(平均值)

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 th

c语言编程-关于C语言字符串的简单题求助

问题描述 关于C语言字符串的简单题求助 进行对输入的字符串重新排列,要求字母在前,数字在后,并不改变字母和数字之间的字符排列顺序. 解决方案 #include void main() { char a[10] = {0}, b[10] = {0}, c[10]={0}; int n = 0, m = 0, k = 0,f = 0; printf("输入字符串:"); gets(a); for(int j = 0; j < 10; j++) { if((a[j] >= 'a'

HDOJ(HDU) 1994 利息计算(简单题目)

Problem Description 为自行解决学费,chx勤工俭学收入10000元以1年定期存入银行,年利率为3.7% .利率 按年计算,表示100元存1年的利息为3.7元.实际上有时提前有时推迟取,因此实际利息按天 计算,1年按365天计算,因此Q天的利息是 本金*3.7/100 *Q/365 存了100天后1年定期年利息提高到3.9%.如将存款提前全取出,再存1年定期.那么前面的 100天只能按活期利息1.7%计算. 100天的利息和本金:10000(1+1.7/100*100/365)

ZOJ 简单题集合

这部分题由于过于简单,属于白送题目,因此把所有特别简单题的合集于此. 1048:统计某人12个月的银行帐户余额的平均数.(简单的令人汗!) Code_1048/*ZOJ 1048: 统计小数的平均数!居然就是12个double求平均数..汗!*/ #include <stdio.h>double balance[12];int main() {  int i;  double sum=0,aver;  for(i=0;i<12;i++)  {   scanf("%lf"

ZOJ 简单题集合(二)

对以下简单题,我同时给出一个我主观认为的难度值(0.1~1.0之间). (1). ZOJ 1072: Microprocessor Simulation. (Difficulty: 0.2) http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1072 微处理器模拟,它含有两个累加器,代码和内存统一寻址,即冯诺依曼结构,比较简单. ZOJ1072_cpp #include <stdio.h>#include <stri

HDOJ 1323 Perfection(简单题)

Problem Description From the article Number Theory in the 1994 Microsoft Encarta: "If a, b, c are integers such that a = bc, a is called a multiple of b or of c, and b or c is called a divisor or factor of a. If c is not 1/-1, b is called a proper di

HDOJ/HDU 2568 前进(简单题)

Problem Description 轻松通过墓碑,进入古墓后,才发现里面别有洞天. 突然,Yifenfei发现自己周围是黑压压的一群蝙蝠,个个扇动翅膀正准备一起向他发起进攻! 形势十分危急! 好在此时的yifenfei已经不是以前那个经常被lemon抢走MM的菜鸟了!面对众多蝙蝠的嗜血狂攻,只见yifenfei使出轻灵的剑法,刷,刷,刷,瞬间搞定-- 现已知yifenfei使用了2招(剑招A和剑招B):剑招A,一招能杀死一半的蝙蝠.但是如果当前的蝙蝠数为奇数,那么就必须先出一招剑招B杀死其中

HDOJ(HDU) 2123 An easy problem(简单题...)

Problem Description In this problem you need to make a multiply table of N * N ,just like the sample out. The element in the ith row and jth column should be the product(乘积) of i and j. Input The first line of input is an integer C which indicate the