zoukankan      html  css  js  c++  java
  • 591 Box of Bricks

    真心好水的题。。。。 只需要求出砖块数目的平均值 ,再用每一摞砖的数目减去平均值 ,只取相减后大于0的或者只取小于零的相加 得到的和就是最少次数

    注意输出两个回车 题目及AC代码如下

      Box of Bricks 

    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 Ÿle n leŸ 50$ and $1 leŸ h_i Ÿle 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.
    

     1 // 591.cpp : 定义控制台应用程序的入口点。
     2 //
     3 #include<stdio.h>
     4 #include<stdlib.h>
     5 #include<string.h>
     6 
     7 
     8 int main(void)
     9 {
    10     int n;
    11     int arr[55];
    12     int sum;
    13     int count = 1;
    14     while (scanf("%d", &n) && n)
    15     {
    16         memset(arr, 0, sizeof(arr));
    17         sum = 0;
    18         for (int i = 0; i < n; i++)
    19         {
    20             scanf("%d", &arr[i]);
    21             sum += arr[i];
    22         }
    23         sum = sum / n;
    24         int times = 0;
    25         for (int i = 0; i < n; i++)
    26         {
    27             if (arr[i] - sum>0) times += (arr[i] - sum);
    28         }
    29         printf("Set #%d
    ", count++);
    30         printf("The minimum number of moves is %d.
    
    ", times);
    31     }
    32     return 0;
    33 }
    34 
    35                 
  • 相关阅读:
    nuxt.js 引入第三方插件报window is not defined
    webstorm 设置js或者html文件自动缩进为4个空格不生效
    调用接口缓存数据
    node 版本更新
    监听2个值的变化,后执行方法
    去除空格
    Redis6详解(四)——配置文件
    Dubbo(一)——
    MybatisPlus(四)——
    数据结构与算法(九)——查找
  • 原文地址:https://www.cnblogs.com/VOID-133/p/3588046.html
Copyright © 2011-2022 走看看