POJ 1477 Box of Bricks C++版

题目链接:http://poj.org/problem?id=1477

题目给出一些高度的木箱,问至少移动多少步,可以移动到同以高度!水题,没什么好说的!

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*Problem: 1477		User: awq123
**Memory: 260K Time: 0MS
**Language: C++ Result: Accepted
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main()
{
int t,i,j=1,d[105];
while(cin>>t&&t;)
{
int sum=0,ans=0;
for(i=0;i<t;i++)
{
cin>>d[i];
sum+=d[i];
}
sum/=t;
for(i=0;i<t;i++)
{
int temp=sum-d[i];
ans+=(temp>0?temp:-temp);
}
cout<<"Set #"<<j<<endl;
cout<<"The minimum number of moves is "<<ans/2<<"."<<endl;
cout<<endl;
j++;
}
}