## 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

Ghost77 dimen
Learning poster
Posts: 67
Joined: Sun Sep 22, 2002 5:40 am
Location: Taiwan
Sample Input
3
1 2 3
5
100 100 100 100 100
4
49 50 49 48
0
Sample Onput
Set #1
The minimum number of moves is 1.

Set #2
The minimum number of moves is 0.

Set #3
The minimum number of moves is 1.

abyssinian
New poster
Posts: 13
Joined: Wed Jan 01, 2003 1:25 pm

### 591 box of bricks....floating point exception??....

err....my program got runtime error : floating point exception
could the problem be that my program can't read specified input that
larger than 9 ...example :
5
11 10 5 6 4 ???

i also got an advice that fflush is forbidden...but if i eliminate fflush...then the program can't read the second input of heights at all....

can someone help me with this probelm?? are there a more simple way to read the input ??

/* @begin_of_source_code */

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

int main(){
int i,total,rata,min,set,n;
int height[60];
char deret[100];

#ifndef ONLINE_JUDGE
freopen("591.in", "r", stdin);
freopen("591.out", "w", stdout);
#endif

set=1;
while(scanf("%d",&min)==1){
if(!min) exit(0);

fflush(stdin);
gets(deret);
n=0;total=0;
for(i=0;deret;++i){
if(deret!=' ') {
height[n]=deret-'0';
total+=height[n];
++n;
}
}
height[n]='\0';

min=0;rata=0;

rata=total/n;
for(i=0;height;i++){
min+=abs(height-rata);
}
min/=2;

printf("Set #%d",set);
printf("\nThe minimum number of moves is %d",min);
++set;

}

return 0;
}
/* @end_of_source_code */

off_algos
New poster
Posts: 29
Joined: Wed Nov 13, 2002 11:37 am
Location: india
hi
receiving the input is pretty simple
first input the number of elements
the dyanamically initialise an array for that many objects
and get the entire set of heights one bye one
later delete this array
and fianlly
receieve n again

my code was as follows:

Code: Select all

``````[cpp]
while(scanf("%d",&n)>0)
{
if(!n)// test for n=0;
break;
int *ptr=new int[n];
for(int i=0;i<n;i++)
cin>>ptr[i];
// process the values
// print the answer
delete []ptr;
}
[/cpp]``````
if u r not at comfy with cpp then malloc function of the c library must help u

still ... if u r just starting and all those said above r jargon to u then
decide on a maximum of number of elements
then input n
input n numbers into this array
process only the first n elements
print the answer
then reinput n
and repeat untill n=0;

in general a floting point execption i believe occurs when u make a mathematically invalid process
eg is division by 0

try this and best'o'luck

abyssinian
New poster
Posts: 13
Joined: Wed Jan 01, 2003 1:25 pm
emmm.....i changed the program so that it can take input correctly
i test the input n output on my computer....they were correct...i even added extra input from the forum...............but i got "wrong answer".....
confused where's wrong.......??... can you help me??

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

int main(){
int i,total,rata,min,set,n;
int height[100];

set=1;

while(scanf("%d",&min)==1){
if(!min) exit(0);

n=min;total=0;
for(i=0;i<min;++i){
scanf("%d",&height);
total+=height;
}
height='\0';
min=0;rata=0;

rata=total/n;
for(i=0;height;i++){
min+=abs(height-rata);
}
min/=2;

printf("Set #%d",set);
printf("\nThe minimum number of moves is %d",min);
++set;

}
return 0;
}

off_algos
New poster
Posts: 29
Joined: Wed Nov 13, 2002 11:37 am
Location: india
i think there r two mistakes in ur program
1.
when u check for height in your for loop, u dont give chance for the input to contain boxes of height zero
hence instead of checking that
u must again check

Code: Select all

``````  i<n
``````

2.
in ur code
min+=abs(height-rata);

is definitely wrong
the question asks u to find the number of bricks that must be moved
and hence change that part into

Code: Select all

``````
if(height[i]-rata>0)
min+=height-rata;

``````
i think u should get an AC

abyssinian
New poster
Posts: 13
Joined: Wed Jan 01, 2003 1:25 pm
em...i've fixed the first one.....
for the second..i think it's not a problem since it will come up with a same anwer.......
mine will read the "height -rata " twice..so in the end i divide the min by 2.....

but i also try your suggestion..and now i get "runtime error (sigsev)...invalid memory reference"...????what's wrong here??

/* @begin_of_source_code */

#include<stdio.h>

int main(){
long int l,u,i,x,max,y,angka;
int sequence[100000];

for(i=0;i<100000;++i){sequence=0;}

#ifndef ONLINE_JUDGE
freopen("371.in", "r", stdin);
freopen("371.out", "w", stdout);
#endif

while(scanf("%li %li",&l,&u)==2){

max=0;
if(l==0 && u==0) break;

for(i=l;i<=u;++i){
if(sequence==0){
angka=x=i;
do{
if (x&1) {x=3*x+1;
}else {x=x>>1;}
++sequence;
if(x<angka && x>=l) {sequence+=sequence[x];break;}
}while(x!=1);
}

if(sequence>max) {max=sequence;y=i;}
}

printf("Between %li and %li, %li generates the longest sequence of %li values.",l,u,y,max);
}

return 0;
}

/* @end_of_source_code */

abyssinian
New poster
Posts: 13
Joined: Wed Jan 01, 2003 1:25 pm
ups ..i'm sorry the program i attach is for 371 ..ackermann functions..
but still i got wrong answer......

the right one is:

/* @begin_of_source_code */

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main(){
long int i,total,rata,min,set,n;
long int height[10000];

#ifndef ONLINE_JUDGE
freopen("591.in", "r", stdin);
freopen("591.out", "w", stdout);
#endif

set=1;
while(scanf("%li",&min)==1){
if(!min) exit(0);

n=min;total=0;
for(i=0;i<min;++i){
scanf("%li",&height);
total+=height;
}
height='\0';

min=0;rata=0;

rata=total/n;

for(i=0;i<n;i++){
if(height>rata){
min+=height-rata;
//min+=abs(height-rata);
}
// min/=2;

printf("Set #%li",set);
printf("\nThe minimum number of moves is %li",min);
++set;

}
return 0;
}
/* @end_of_source_code */

abyssinian
New poster
Posts: 13
Joined: Wed Jan 01, 2003 1:25 pm
i got accepted...but P.E.???

/* @begin_of_source_code */

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

int main(){
long int i,total,rata,min,set,n;
long int height[10000];

#ifndef ONLINE_JUDGE
freopen("591.in", "r", stdin);
freopen("591.out", "w", stdout);
#endif

set=1;
while(scanf("%li",&min)==1){
if(!min) exit(0);

n=min;total=0;
for(i=0;i<min;++i){
scanf("%li",&height);
total+=height;
}
height='\0';

min=0;rata=0;

rata=total/n;

for(i=0;i<n;i++){
min+=abs(height-rata);
}
min/=2;

printf("Set #%li\n",set);
printf("The minimum number of moves is %li.\n",min);
++set;

}
return 0;
}
/* @end_of_source_code */

off_algos
New poster
Posts: 29
Joined: Wed Nov 13, 2002 11:37 am
Location: india

### simple

the problem statement asks you to output a blank line after each set of output

Almost Human
Learning poster
Posts: 93
Joined: Sun Jan 12, 2003 3:30 pm

### 591 - Box of Bricks ( I have a little problem )

why is it P.E. ? please help me....

Code: Select all

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

int main ( )
{
int addorsub , average , input , i , input2[100] , problem = 0 ;

/*  freopen ( "591.in" , "r" , stdin ) ;
freopen ( "591.out" , "w" , stdout ) ;*/

while ( 1 )
{
scanf ( "%i" , &input ) ;
if ( !input ) break ;
problem ++ ;

for ( i = average = 0 ; i < input ; i ++ )
{
scanf ( "%i" , &input2[i] ) ;

average += input2[i] ;
}

average /= input ;

for ( i = addorsub = 0 ; i < input ; i ++ )
{
addorsub += input2[i] > average ? ( input2[i] - average ) : ( average - input2[i] ) ;
}

printf ( "Set #%i\nThe minimum number of moves is %i." , problem , addorsub / 2 ) ;
if ( !feof ( stdin ) ) printf ( "\n" ) ;
}
return 0 ;
}
``````

Farid Ahmadov
Experienced poster
Posts: 131
Joined: Thu Apr 17, 2003 8:39 am
Location: Baku, Azerbaijan
Hello.
Try to write it without
if ( !feof ( stdin ) ) printf ( "\n" ) ;

just write printf( "\n"); and all will be OK.
Bye.
_____________
NO sigNature

Almost Human
Learning poster
Posts: 93
Joined: Sun Jan 12, 2003 3:30 pm
I have tried... but it is still got P.E.

thanks anyway...

J&Jewel
New poster
Posts: 50
Joined: Thu Jul 31, 2003 10:43 am
Location: Daffodil University,Dhaka,Bangladesh
Contact:
I also got P.E.
I donn`t know Why?
I hate Wrong Answer!

Master
Learning poster
Posts: 82
Joined: Thu Oct 10, 2002 1:15 pm
Location: St. Johns, Canada
Contact:
I got AC
I use the following
[cpp]
while(con)
{
......
.....
.....
.....
printf("Set.. \n");
printf("The .... %d,\n\n",...);
scanf("...");
}
[/cpp]

M H Rasel
CUET Old Sailor

Jewel of DIU
New poster
Posts: 32
Joined: Thu Jul 31, 2003 6:21 am
Location: Daffodil Univ, Bangladesh
Contact:

### 591- AC but Why PE

I got AC in this problem in 0.002 sec but i got PE. I don't know why it got PE. See my code
[c]
printf("Set #%d\n",set+1);
printf("The minimum number of moves is %d.\n",change);
set++;
[/c]
Hate WA
Visit phpBB!