k-11

    xiaoxiao2021-03-25  90

    Description 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 <= 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. 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.

    题意描述:

    就是把几组不同高的砖盒移到同一高度,求需要移动的最小次数。

    解题思路:

    由题意可得,用所有的砖的数目除以组数,得到的平均数便是最后的高度,用高于平均数的组数减去平均数,将所有减到的结果相加,便是最后的结果。

    解题细节:

    注意输出的一些细节,单词之间要有空格,并且最后要有"."。

    代码:

    #include<bits/stdc++.h> using namespace std; int main() { int i,j,k=0,m,n,aver; int sum=0,sum1=0; vector<int>v; while(cin>>n) { sum=0;sum1=0; k++; if(n==0)break; v.clear(); for(i=0;i<n;i++) { cin>>m; v.push_back(m); sum+=m; } aver=sum/n; for(i=0;i<v.size();i++) { if(v[i]>aver) sum1+=(v[i]-aver); } cout<<"Set #"<<k<<endl; cout<<"The minimum number of moves is"<<' '<<sum1<<"."<<endl; cout<<endl; } return 0; }心得:

    解题时要利用题目给出的条件,比如此题中砖的总数可以整除于组数,同时还要注意一些题目输出的要求。

    转载请注明原文地址: https://ju.6miu.com/read-25622.html

    最新回复(0)