## 591 - Box of Bricks

Moderator: Board moderators

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
You forgot to initialize 'average'.

scott1991
New poster
Posts: 28
Joined: Fri Dec 07, 2007 11:41 pm
i thought i did with

Code: Select all

``int n,h=0,average=0,moves=0,set=1;``
if this wasn't initializing 'average', what is please? cheers.

Tarif
New poster
Posts: 3
Joined: Wed Mar 26, 2008 6:44 am
Contact:

### Re: acm-591 prob

I need a bit help in this. If the number of bricks in one line is more than 100, what would happen? Should we just ignore that input and go for the next one or we escape that set of inputs. please help!

Regards
Tarif
Tarif Ezaz
North South University
12 Kamal Ataturk Avenue, Dhaka

Raffan_033
New poster
Posts: 2
Joined: Tue Feb 12, 2008 8:52 pm

### Re: acm-591 prob

Can any one plz help with the code?i donno whats wrong.
i'm stuck for a very very very long time with this problem.

#include<stdio.h>

int main(void)
{
int stack_height[50]={0};
int n,total_height,average_height,move,count=1,i;

while(scanf("%d",&n)==1)
{

if((n<1)||(n>50))
break;

move=0;
total_height=0;
for(i=0;i<n;i++)
{
scanf("%d",&stack_height[i]);

if((stack_height[i]<1)||(stack_height[i]>100))
break;;

total_height+=stack_height[i];
}

if((total_height%n)!=0)
break;

average_height=total_height/n;

for(i=0;i<n;i++)
{
if(stack_height[i]>average_height)
move+=(stack_height[i] - average_height);
}

printf("Set #%d\n",count);
printf("The minimun number of moves is %d.\n",move);

count++;
}
return 0;
}

Raffan

vinocit
New poster
Posts: 10
Joined: Mon Oct 13, 2008 10:11 am

### Re: 591

Hi does anyone know wats wrong in my code
I am getting WA

Code: Select all

``````Removed after AC
``````
Last edited by vinocit on Wed Nov 12, 2008 7:31 am, edited 1 time in total.

porker2008
New poster
Posts: 21
Joined: Wed Oct 08, 2008 7:04 am

### To "vinocit"

Hi.
This is an easy problem.

Code: Select all

``Output a blank line after each set. ``
That's why you get WA.
You didn't put any blank line after them.

I have tested for you.
if this line

Code: Select all

``cout<<"The minimum number of moves is "<<mov<<".\n";``
changes to

Code: Select all

``cout<<"The minimum number of moves is "<<mov<<".\n\n";``

BTW,"Raffan_033" also forgot to put a blank line after each case. However, after I add it for him/her, it still get WA.

Best regards.

vinocit
New poster
Posts: 10
Joined: Mon Oct 13, 2008 10:11 am

### Re: 591

Thanks i got ACed

Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Contact:

### why wa??

i got wa.

but i cant understand why!!!!!
anyone help plz.
my code

Code: Select all

``````
#include<stdio.h>
#include<stdlib.h>

int main()

{
int s[100];
int n;
int sum;
int avg;
int mv;
int i,ncase=0;

while(scanf("%d",&n)==1)
{
if(n==0)
break;

ncase++;
sum=0;
for(i=0;i<n;i++){
scanf("%d",&s[i]);
sum=sum+s[i];
}

avg=sum/n;

mv=0;
for(i=0;i<n;i++)
{
if(s[i]<avg)

mv=mv+(avg-s[i]);

}
printf("Set #%d\n",ncase);
printf("The minimum number of moves is %d\n\n",mv);

}

return 0;
}
``````
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359

Articuno
Learning poster
Posts: 78
Joined: Sun Nov 30, 2008 5:00 pm

### Re: 591

Your code is OK. But you forgot to print a '.' in the output.
May be tomorrow is a better day............

Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Contact:

### Re: 591

thanx to articuno for ur help.
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359

metalcarryon
New poster
Posts: 2
Joined: Mon Feb 09, 2009 7:41 pm

### Re: 591

I got RE why? thanks everyone!

Code: Select all

``````#include <stdio.h>
#include <stdlib.h>

int compare (const void * a, const void * b);

int compare (const void * a, const void * b)  //compara inteiros
{
return ( *(int*)a - *(int*)b );
}

int main ()
{
int i,j,k,temp;
int vetor[55];
int quantum = 0;
int cont = 0;
int vezes = 0;

cont = 0;
temp = 0;
quantum = 0;

scanf("%d",&temp);
vetor[j] = temp;
quantum = quantum + temp;		//estabeleço o quantum
}
for(k=0;k<55;k++){                   //for semi-infinito

cont = cont + 1;

}

if(vetor[quantidade-1]== vetor[0]) break;      //se primeiro elemento = ultimo, entao acabou!
}															//terminda for semi-infinito
vezes = vezes + 1;
printf("Set #%d\nThe minimum number of moves is %d.\n",vezes,cont);

}													//termina o while que comanda o programa

return 0;
}			//termina main()

``````

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

### Re: 591

Code: Select all

``````quantum = quantum/quantidade;      <------ divide by 0 ???
``````

mhn
New poster
Posts: 4
Joined: Mon Jun 22, 2009 7:05 am

### Re: 591

i m getting wa..y??can anyone plz help me....

#include<stdio.h>

int main(){

int x[100],n,sum,a,b,i,j=1;

while(scanf("%d",&n)==1)
{sum=0;
b=0;
if(n!=0 && n>=1 && n<=50)
{
for(i=0;i<n;i++)
{
scanf("%d",&x);
if(x>=1 && x<=100)
{
sum=sum+x ;
}
else break;
}
if(sum%n==0)
{
a=sum/n;
for(i=0;i<n;i++)
{
if(x>a) b=b+x-a;
else continue;
}
} else break;

printf("Set #%d\n",j++);
printf("The minimum number of moves is %d\n",b);
}

else break;
}
return 0;
}

valkov
New poster
Posts: 20
Joined: Tue Jul 20, 2010 3:11 pm

### Re: 591

Hi!
Just got AC
Some tips:

Code: Select all

``````1. Find the average height:
total sum / number of towers
2. Sort the towers in asc order
(for C++ guys, use <algorithm> sort() with vector or array if you want to get it done quickly)
3. For each tower with height > average:
moves += tower height - average
4. Print the moves as specified in the problem ( read carefully and output blank line after EACH set )``````
Wish you luck

faiem
New poster
Posts: 14
Joined: Fri Aug 13, 2010 12:22 pm

### Re: 591

why u r sorting???
it is possible without sorting...and very fast...