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

  • 相关阅读:
    nodejs学习笔记
    php操作mysql数据库
    HTML5 新特性总结
    万恶的浏览器兼容问题
    图标字体使用方法
    托管代码
    进程间通信,把字符串指针作为参数通过SendMessage传递给另一个进程,不起作用
    利用自定义消息处理函数的WPARAM或LPARAM参数传递指针
    自定义消息中如果需要定义WPARAM和LPARAM,该怎么使用和分配?
    提高VS2010运行速度的技巧+关闭拼写检查
  • 原文地址:https://www.cnblogs.com/damaoranran/p/8711934.html
Copyright © 2011-2022 走看看