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

  • 相关阅读:
    vscode 缩进改为2空格
    js canvas图片压缩
    vue 父子组件通信
    vue prop 传递数据
    vue报错信息
    android.useDeprecatedNdk=true
    app:compileDebugNdk,NDK
    Kotlin来了
    design
    com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: android/support/annotation/ColorRes.class
  • 原文地址:https://www.cnblogs.com/damaoranran/p/8711934.html
Copyright © 2020-2023  润新知