10038 - Jolly Jumpers

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

Moderator: Board moderators

Post Reply
User avatar
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel » Tue Oct 12, 2004 9:51 am

Hi Hiro,

your code seems to be in perfect condition but the breaking condition of the while loop seems to be a little disarrayed.

.. I slightly modified that part and your code got AC.

Change
[c]
while ((c = getchar()) != '\n')
{
cin.putback(c);

int n;
cin >> n;
[/c]

to
[c]
int n;
while (cin>>n)
{
// ........
}
[/c]


and it should get AC. :D

hiro
New poster
Posts: 5
Joined: Sun Oct 10, 2004 11:34 pm

Post by hiro » Tue Oct 12, 2004 2:07 pm

Hi Sohel!!!
I modified my code as you suggested and i got AC.
Thank you a lot.... i couldn't see what was wrong with my code.....
But i still don't understand why the code:
while ((c = getchar()) != '\n')
{
cin.putback(c);

int n;
cin >> n;
....
}
don't get accepted?
I tested your suggestion in my computer and when a press only enter, the program doesn't finish. But when i redirect my standard input to a file, it works perfectly. Why does this happens? I am using cygnus g++ under windows Xp.
The input of the problem requires that you press enter if you finish the test cases isnt it? I am having big problems with my input!!
If you could help me!!
Thanks!!

User avatar
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel » Tue Oct 12, 2004 2:17 pm

I'm not quite sure but I think there could be some extra lines in the judge's data after the very last case.

It is better to avoid getchar() and gets() when it's not needed.

hiro
New poster
Posts: 5
Joined: Sun Oct 10, 2004 11:34 pm

Post by hiro » Tue Oct 12, 2004 3:06 pm

Thank you!!!

User avatar
habibiamin
New poster
Posts: 8
Joined: Tue Oct 12, 2004 1:54 pm
Location: Iran-Tehran
Contact:

10038 Why RTE (Runtime Error)

Post by habibiamin » Sat Oct 16, 2004 10:54 am

This is my solution for problem 10038 {Jolly Jumpers}
but I don't know why RTE :oops:
Please help me !
Thanks
[cpp]
//http://acm.uva.es/p/v100/10038.html
#include<stdio.h>
int main()
{
long long int Jolly[3000];
long long int Sequence[3001];
long long int n,i,j,minus;
while(scanf("%lld",&n)==1)
{
for(i=1;i<=n;i++)
{
scanf("%lld",&Sequence);
Jolly=1;
}
long long int r=1;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
minus=Sequence-Sequence[j];
if(minus<0)minus*=-1;
if(minus>=1&&minus<=(n-1))
Jolly[minus]=0;
else
{r=0;break;}
}

for(i=1;i<=(n-1);i++)
if(Jolly==1)
{r=0;break;}
if(r)
printf("Jolly\n");
else
printf("Not jolly\n");



}
return 0;
}
[/cpp]

User avatar
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel » Sun Oct 17, 2004 9:56 am

Hi habibiamin,

A part of your code:
[c]
long long int Jolly[3000];
long long int Sequence[3001];
long long int n,i,j,minus;
while(scanf("%lld",&n)==1)
{
for(i=1;i<=n;i++)
{
scanf("%lld",&Sequence);
Jolly=1;
}
[/c]

If n=3000 then you are assigning Jolly[3000] as 1 but your Jolly array can handle from 0 to 2999 and hence the cause of RTE.. :o

.. You can get rid of RTE by enlarging the array size but I must warn that we will get WA. :(


and try to avoid long long when it's not needed. :wink:

User avatar
habibiamin
New poster
Posts: 8
Joined: Tue Oct 12, 2004 1:54 pm
Location: Iran-Tehran
Contact:

Post by habibiamin » Sun Oct 17, 2004 12:21 pm

Thanks dear sohel...

I do your suggestion and get WA :oops: ...I'll try to get AC

bye :wink:

simondhaka
New poster
Posts: 4
Joined: Sat Nov 13, 2004 12:17 pm
Location: Dhaka,Bangladesh
Contact:

10038

Post by simondhaka » Sat Nov 13, 2004 12:26 pm

I dont understand why do I get WA . Can any one check this code plz:

[c]
#include<stdio.h>
#include<stdlib.h>

void main(){
long int n,i,f1,f2,diff,bool,array[3005]={0};

FILE *infile,*outfile;
infile=stdin;
outfile=stdout;

while(1){
if(feof(infile)) break;

fscanf(infile,"%ld",&n);
fscanf(infile,"%ld",&f1);

for(i=0;i<n;i++)
array=0;

for(i=1;i<n;i++){
fscanf(infile,"%ld",&f2);
diff=abs(f1-f2);
if (diff<n && diff>0)
array[diff]=1;
f1=f2;
}
bool=1;
for(i=1;i<n;i++){
if(array==0){
bool=0;
}
}

if (bool==0)
fprintf(outfile,"Not Jolly\n");
else
fprintf(outfile,"Jolly\n");

}
}[/c] :( :(

simondhaka
New poster
Posts: 4
Joined: Sat Nov 13, 2004 12:17 pm
Location: Dhaka,Bangladesh
Contact:

10038

Post by simondhaka » Sat Nov 13, 2004 12:27 pm

I dont understand why do I get WA . Can any one check this code plz:

[c]
#include<stdio.h>
#include<stdlib.h>

void main(){
long int n,i,f1,f2,diff,bool,array[3005]={0};

FILE *infile,*outfile;
infile=stdin;
outfile=stdout;

while(1){
if(feof(infile)) break;

fscanf(infile,"%ld",&n);
fscanf(infile,"%ld",&f1);

for(i=0;i<n;i++)
array=0;

for(i=1;i<n;i++){
fscanf(infile,"%ld",&f2);
diff=abs(f1-f2);
if (diff<n && diff>0)
array[diff]=1;
f1=f2;
}
bool=1;
for(i=1;i<n;i++){
if(array==0){
bool=0;
}
}

if (bool==0)
fprintf(outfile,"Not Jolly\n");
else
fprintf(outfile,"Jolly\n");

}
}[/c] :( :(

Mohammad Mahmudur Rahman
Experienced poster
Posts: 154
Joined: Sat Apr 17, 2004 9:34 am
Location: EEE, BUET

Post by Mohammad Mahmudur Rahman » Sat Nov 13, 2004 7:11 pm

The problem lies in the way you are pursing the input. The input is not terminating & ultimately resulting in a WA.
Use -
[c]
while(fscanf(infile,"%ld",&n)!=EOF)
{

fscanf(infile,"%ld",&f1);
.....
.....
}

[/c]

& don't forget to change the Not Jolly to Not jolly in the printf(). 8)
Last edited by Mohammad Mahmudur Rahman on Sun Nov 14, 2004 6:21 pm, edited 1 time in total.
You should never take more than you give in the circle of life.

simondhaka
New poster
Posts: 4
Joined: Sat Nov 13, 2004 12:17 pm
Location: Dhaka,Bangladesh
Contact:

Thanku

Post by simondhaka » Sun Nov 14, 2004 12:59 am

:P :lol: :wink: :D :)

Thank you. I got AC. It was really a "Easy Jhamela Problem" for me. Thank you very much.

Good Bye Ramadan
&
Happy EID

Mohammad Mahmudur Rahman
Experienced poster
Posts: 154
Joined: Sat Apr 17, 2004 9:34 am
Location: EEE, BUET

Post by Mohammad Mahmudur Rahman » Sun Nov 14, 2004 6:19 pm

Nice to know that u've got AC. :D Happy Eid to u 2.
You should never take more than you give in the circle of life.

Niaz
Learning poster
Posts: 77
Joined: Fri Dec 17, 2004 11:06 am
Location: East West University, Dhaka, Bangladesh
Contact:

Post by Niaz » Sat Dec 18, 2004 5:21 pm

hmmm. Great Programmer and also Great Helper. Well done Sohel. :)
Please join The ACM Solver Group at Yahoo
http://groups.yahoo.com/group/acm_solver/

jambon_vn
New poster
Posts: 15
Joined: Wed Sep 29, 2004 6:03 am

Post by jambon_vn » Mon Dec 20, 2004 8:25 am

Man, I have the same problem.
I used cin.peek first and get WA. It is AC after changing to cin >> N.

Thank so much. However don't know what is going on.

User avatar
Rocky
Experienced poster
Posts: 124
Joined: Thu Oct 14, 2004 9:05 am
Contact:

10038

Post by Rocky » Thu Jan 06, 2005 6:04 am

I do not test your code but i can suggest you to do all the irretation to find the distance between two number and check what they say to check the all distance (1 to n-1)

by the way if the n=4
then the if the distance is 1 2 3 or 3 1 2 or 3 2 1 all is jolly

GOOD LUCK

Post Reply

Return to “Volume 100 (10000-10099)”