Page 1 of 6
Posted: Sat Nov 30, 2002 6:16 pm 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.

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

Posted: Fri Jan 03, 2003 10:37 am 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;
char deret;

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

Posted: Fri Jan 03, 2003 5:27 pm
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
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
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

Posted: Sat Jan 04, 2003 4:51 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;

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;
}

Posted: Sun Jan 05, 2003 1:40 pm
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
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

Posted: Fri Jan 10, 2003 7:26 am
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;

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 */

Posted: Fri Jan 10, 2003 7:37 am
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;

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

Posted: Sat Jan 11, 2003 7:36 am 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;

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

### simple

Posted: Sun Jan 12, 2003 6:11 am
the problem statement asks you to output a blank line after each set of output

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

Posted: Wed Apr 23, 2003 12:50 pm

Code: Select all

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

int main ( )
{
int addorsub , average , input , i , input2 , 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 ;
}
``````

Posted: Wed Apr 23, 2003 6:29 pm
Hello.
Try to write it without
if ( !feof ( stdin ) ) printf ( "\n" ) ;

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

Posted: Thu Apr 24, 2003 3:30 am
I have tried... but it is still got P.E.

thanks anyway...

Posted: Thu Aug 07, 2003 10:15 am
I also got P.E.
I donn`t know Why?

Posted: Thu Aug 14, 2003 6:53 am
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

### 591- AC but Why PE

Posted: Tue Oct 28, 2003 10:34 am
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]