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);
     }
    }

  • 相关阅读:
    ExtJs2.0学习系列(1)Ext.MessageBox
    PDF加水印
    ExtJs2.0学习系列(2)Ext.Panel
    负载均衡(续)
    位运算设置权限续(转)
    Excel导出问题解决方案(导出时前面的0自动被去掉)
    SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
    位运算设置权限
    WCF开发实战系列一:创建第一个WCF服务
    通过SQL Server的位运算功能巧妙解决多选查询
  • 原文地址:https://www.cnblogs.com/damaoranran/p/8711934.html
Copyright © 2011-2022 走看看