1251:很多堆高低不等的砖块,每次移动一个,为了弄成等高,求最少步数。
Sample Input
6
5 2 4 1 7 5
0
Sample Output
Set #1
The minimum number of moves is 5.
简单题。先求平均,凡是大于平均的部分都移走,即得结果。
Sample Input
6
5 2 4 1 7 5
0
Sample Output
Set #1
The minimum number of moves is 5.
简单题。先求平均,凡是大于平均的部分都移走,即得结果。
#include<stdio.h>
#include<iostream>
using namespace std;
int heap[50];
int main()
{
int n;
int sum;
int step;
int level;
int count=1;
while(1)
{
cin>>n;
sum=0;
step=0;
if(n==0)
break;
for(int i=0;i<n;i++)
{
cin>>heap[i];
sum+=heap[i];
}
level = sum/n;
for(int i=0;i<n;i++)
{
if(heap[i]>level)
step+=heap[i]-level;
}
cout<<"Set #"<<count<<endl;
cout<<"The minimum number of moves is "<<step<<"."<<endl;
cout<<endl;
count++;
}
6331

被折叠的 条评论
为什么被折叠?



