zoukankan      html  css  js  c++  java
  • E

    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?

    InputThe 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.
    OutputFor 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

    5

    这道题非常简单,但是输出格式有点儿坑,尤其对于我这种才坑的人来说。在这里批评很多博客主只是为了经验!根本不管这道题的考察的是什么。
    这道题注重的是输出格式。要保证输入0前没有空行,而在两组数据之间必须有空行。(想想都是气)
    代码如下:(大家注意看如何保障输入0时没空行的操作)

    #include<iostream>
    using namespace std;
    int num[51];

    int main()
    {
     int n;
     int t = 0;
     while (cin >> n&&n)
     {
      if (t != 0)cout << endl;
      t = 1;
      for (int i = 0; i < n; i++)
      {
       cin >> num[i];
      }
      int sum = 0;
      for (int i = 0; i < n; i++)
      {
       sum += num[i];
      }
      sum = sum / n;
      int Sum = 0;
      for (int i = 0; i < n; i++)
      {
       if (num[i]>sum)
       {
        Sum += num[i] - sum;
       }
      }
      printf("%d ", Sum);
     }
    }

  • 相关阅读:
    js实现好看的图案 加勒比海盗(php拍黄片)
    网站优化的几点 下
    php算法 快速排序 选择算法 冒泡算法
    javascript中null和undefined的区别
    tp3 上传图片出现上传根目录不存在!请尝试手动创建:./Public/Uploads/ 错误解决思路
    网站优化的几点 上
    tp5框架 报错非法请求:admin/index/index
    通过response向服务器用Io流写入图片
    解决客户端向服务器写中文时乱码问题
    setInterval()设置页面5,4,3,2,1秒后跳转
  • 原文地址:https://www.cnblogs.com/damaoranran/p/8711934.html
Copyright © 2011-2022 走看看