591 - Box of Bricks

All about problems in Volume 5. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Mon Mar 10, 2008 1:21 am

You forgot to initialize 'average'.

scott1991
New poster
Posts: 28
Joined: Fri Dec 07, 2007 11:41 pm

Post by scott1991 » Mon Mar 10, 2008 8:35 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
Location: Bangladesh
Contact:

Re: acm-591 prob

Post by Tarif » Thu Jul 10, 2008 12:49 pm

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
Undergraduate student
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

Post by Raffan_033 » Sat Jul 12, 2008 9:40 pm

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

Post by vinocit » Mon Oct 13, 2008 11:30 am

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"

Post by porker2008 » Tue Nov 04, 2008 1:08 pm

Hi.
This is an easy problem.
But please be careful about its output standard.

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";
your code will be accepted.

So please remember to remove your code because it's nearly accepted.

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

Post by vinocit » Wed Nov 12, 2008 7:27 am

Thanks i got ACed :D

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

why wa??

Post by sazzadcsedu » Thu Dec 18, 2008 9:29 pm

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
For Hints: http://salimsazzad.wordpress.com

Articuno
Learning poster
Posts: 78
Joined: Sun Nov 30, 2008 5:00 pm
Location: IUT-OIC, Dhaka, Bangladesh

Re: 591

Post by Articuno » Fri Dec 19, 2008 8:23 am

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

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

Re: 591

Post by sazzadcsedu » Fri Dec 26, 2008 9:21 pm

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

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

Re: 591

Post by metalcarryon » Tue Feb 17, 2009 3:40 am

I got RE why? thanks everyone! :wink:

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 quantidade = 0;
  int vetor[55];
  int quantum = 0;
  int cont = 0;
  int vezes = 0;
  
  while(scanf("%d",&quantidade)==1){                 //começa o while
  cont = 0;
  temp = 0;
  quantum = 0;
  for(j = 0;j<quantidade;j++){
  		
		  scanf("%d",&temp);
		  vetor[j] = temp;
		  quantum = quantum + temp;		//estabeleço o quantum
		  }
		  quantum = quantum/quantidade;		  		   		   	 	  	  						 		  
  for(k=0;k<55;k++){                   //for semi-infinito
  
  qsort (vetor, quantidade, sizeof(int), compare);
  
  while(vetor[quantidade-1]>quantum){
		vetor[quantidade-1] = vetor[quantidade-1] - 1;
		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

Post by helloneo » Wed Feb 18, 2009 4:46 pm

When quantidade = 0 ..

Code: Select all

quantum = quantum/quantidade;      <------ divide by 0 ???
:-)

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

Re: 591

Post by mhn » Thu Jul 02, 2009 4:27 pm

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

Post by valkov » Mon Sep 06, 2010 2:33 pm

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

Post by faiem » Tue Sep 07, 2010 8:50 pm

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

Post Reply

Return to “Volume 5 (500-599)”