Thursday, January 24, 2008

Box of bricks : Problem No.591

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
int total,x,k=0,t=0,j=1,i=0;
int nos[50];

cin>>total;

while(total!=0)
{

for(i=0;i<total;i++)
{
cin>>x;
nos[i]=x;

t=t+nos[i];
}
t=t/total;

for(i=0;i<total;i++)
{
if((nos[i]>t) || (nos[i]<t))
{
k = k + abs(nos[i]-t);
}
}

cout<<"Set #"<<j<<endl<<"The minimum number of moves is "<<k/2<<"."<<endl<<endl;

t=0;
k=0;
j++;
cin>>total;

}
}

Problem submitted with runtime = 0.000

Do comment for better algorithm.

No comments: