《ACM 书中题目》 k

来源:互联网 发布:mac视频导入iphone 编辑:程序博客网 时间:2024/06/03 02:14
  • 题目

    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?

  • 理解
    求出序列的平均数,超出平均数的部分就是要移动的块数

  • AC代码

#include<iostream>using namespace std;int main(){    int i,j,k,l,sum=0,mv;    int a[55],b[55];    for(j=1;cin>>l&&l!=0;j++)    {        sum=0;        mv=0;        for(i=0;i<l;i++)        {            cin>>a[i];            sum+=a[i];        }        sum/=l;        for(i=0;i<l;i++)        {            if((a[i]-sum)>0)                mv+=(a[i]-sum);        }        cout<<"Set #"<<j<<endl<<"The minimum number of moves is "<<mv<<".\n"<<endl;    }}
  • 总结
    刚开始两遍代码全部Presentation Error.
    后来发现题目最后一句写着:Output a blank line after each set.
    读题不够细致,主要还是对英文题目不太适应。
0 0
原创粉丝点击