Problem E: Jolly Jumpers
A sequence of n > 0 integers is called a jolly jumper if the absolute
values of the difference between successive elements take on all the
values 1 through n-1. For instance,
1 4 2 3
is a jolly jumper, because the absolutes differences are 3, 2, and 1
respectively. The definition implies that any sequence of a single
integer is a jolly jumper. You are to write a program to determine
whether or not each of a number of sequences is a jolly jumper.
Input
Each line of input contains an integer n <= 3000 followed by n integers
representing the sequence.
Output
For each line of input, generate a line of output saying "Jolly" or "Not
jolly".
Sample Input
4 1 4 2 3
5 1 4 2 -1 6
Sample Output
Jolly
Not jolly
is:
#include <stdio.h>
#define MAXN 2999
#define MAXDIFF 2999
/* read: read n numbers into a */
void read(int n, int a[])
{
int i;
for (i = 0; i < n; i++)
scanf("%d", a + i);
}
int absdiff(int a, int b)
{
return a>b ? a-b : b-a;
}
int isjolly(int n, int a[])
{
if (n == 1)
return 1;
else {
int takenon[MAXDIFF], i;
for (i = 1; i <= n-1; i++)
takenon[i] = 0;
for (i = 0; i < n-1; i++)
takenon[absdiff(a[i], a[i+1])] = 1;
for (i = 1; i <= n-1 && takenon[i]; i++)
;
return i == n ? 1 : 0;
}
}
int main()
{
int n, num[MAXN];
int i;
while (scanf("%d", &n)) {
if (n) {
read(n, num);
if (isjolly(n, num))
puts("Jolly");
else
puts("Not jolly");
}
}
return 0;
}
How do I get my program to detect an empty line of input? That is, where
just enter is pressed?
TIA,
Albert
>My current code to this problem:
An ACM Prog. Comp. problem??
>How do I get my program to detect an empty line of input? That is, where
>just enter is pressed?
First read in the line with fgets(), check its return value and line read
for end of file or an empty line, then use sscanf() on the line read.
--
Chris.
Albert wrote:
> My current code to this problem:
>
> Problem E: Jolly Jumpers
> A sequence of n > 0 integers is called a jolly jumper if the absolute
> values of the difference between successive elements take on all the
> values 1 through n-1. For instance,
>
> 1 4 2 3
>
> is a jolly jumper, because the absolutes differences are 3, 2, and 1
> respectively. The definition implies that any sequence of a single
> integer is a jolly jumper. You are to write a program to determine
> whether or not each of a number of sequences is a jolly jumper.
> Input
>
> Each line of input contains an integer n <= 3000 followed by n integers
> representing the sequence.
> Output
> For each line of input, generate a line of output saying "Jolly" or "Not
> jolly".
> Sample Input
>
> 4 1 4 2 3
> 5 1 4 2 -1 6
>
> Sample Output
>
> Jolly
> Not jolly
>
OI problem?! Where do you find it?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iEYEARECAAYFAktNsrwACgkQm4klUUKw07C6wwCePVtwvz2JiKIh+gWVBTf/O/oi
MoYAn1bW2MWBJKppS9sDKvvPdqHDrJ/h
=kF5R
-----END PGP SIGNATURE-----
> How do I get my program to detect an empty line of input? That is, where
> just enter is pressed?
There is a way to do that, here:
http://www.mindspring.com/~pfilandr/C/fscanf_input/fscanf_input.c
--
pete
Here's an example with numeric input:
http://www.mindspring.com/~pfilandr/C/fscanf_input/grade.c
--
pete
You've had your question answered but I suspect it is the wrong
question. The problem statement says that there will be no blank
lines. What probably need is a test for the end of the input or, more
helpfully, a test for the input having succeeded. Did this not come
up before in another such problem?
You will be better off writing:
while (scanf("%d", &n) == 1) {...}
As you have it the loop keeps going even if scanf returns EOF. When
it does, the test on n later is not helpful.
--
Ben.
> Albert wrote:
>
>> Problem E: Jolly Jumpers
>> A sequence of n > 0 integers is called a jolly jumper if the absolute
>> values of the difference between successive elements take on all the
>> values 1 through n-1. For instance,
>>
>> 1 4 2 3
>>
>> is a jolly jumper, because the absolutes differences are 3, 2, and 1
>> respectively. The definition implies that any sequence of a single
>> integer is a jolly jumper. You are to write a program to determine
>> whether or not each of a number of sequences is a jolly jumper.
>> Input
>>
>> Each line of input contains an integer n <= 3000 followed by n integers
>> representing the sequence.
>>
> OI problem?! Where do you find it?
--
Ben.
Unfortunately, it doesn't say what the range of those integers
are.
> Output
> For each line of input, generate a line of output saying
> "Jolly" or "Not jolly".
...
> #include <stdio.h>
>
> #define MAXN 2999
There may be 3000 integers!
> #define MAXDIFF 2999
MAXDIFF is based on MAXN.
Also note, the difference may be outside that, e.g.
3 2 1 32767
> /* read: read n numbers into a */
> void read(int n, int a[])
> {
> int i;
>
> for (i = 0; i < n; i++)
> scanf("%d", a + i);
>
> }
>
> int absdiff(int a, int b)
> {
> return a>b ? a-b : b-a;
Note that absdiff(0, INT_MIN) can potentially overflow.
> }
>
> int isjolly(int n, int a[])
> {
> if (n == 1)
> return 1;
> else {
> int takenon[MAXDIFF], i;
> for (i = 1; i <= n-1; i++)
> takenon[i] = 0;
You do this with an initialiser...
int takenon[MAXDIFF] = { 0 }, i;
Though I don't like allocating automatic (local) variables
larger than 256 bytes.
>
> for (i = 0; i < n-1; i++)
> takenon[absdiff(a[i], a[i+1])] = 1;
What if the absolute difference is not in the range 0..2999?
> for (i = 1; i <= n-1 && takenon[i]; i++)
> ;
> return i == n ? 1 : 0;
> }
>
> }
>
> int main()
> {
> int n, num[MAXN];
> int i;
>
> while (scanf("%d", &n)) {
while (scanf("%d", &n) == 1) {
> if (n) {
> read(n, num);
> if (isjolly(n, num))
> puts("Jolly");
> else
> puts("Not jolly");
> }
> }
>
> return 0;
>
> }
#include <limits.h>
#include <stdio.h>
#define MAX_N 3000
#define MAX_D (MAX_N - 1)
int diff(int a, int b)
{
int d;
if (INT_MIN + 1 == -INT_MAX) /* 2's complement? */
{
if ( (a == 0 && b == INT_MIN)
|| (a == INT_MIN && b == 0) )
return 0;
}
d = (a < b) ? b - a : a - b;
return (MAX_D < d) ? 0 : d;
}
int main(void)
{
int N, i, jolly;
static int n[MAX_N];
static char d[MAX_D + 1];
while (scanf("%d", &N) == 1 && 1 <= N && N <= MAX_N)
{
for (i = 0; i < N; i++)
if (scanf("%d", &n[i]) != 1)
return 0;
for (i = 0; i < MAX_D + 1; i++)
d[i] = 0;
for (i = 0; i < N - 1; i++)
d[ diff(n[i], n[i + 1]) ] = 1;
jolly = (d[0] == 0);
for (i = 1; i <= N - 1; i++)
jolly = (jolly && d[i] == 1);
puts(jolly ? "Jolly" : "Not jolly");
}
return 0;
}
--
Peter
3000 numbers have 2999 differences.
Alright: but why after redirecting an input file
0
4 1 4 2 3
5 1 4 2 -1 6
1 1052
to what I believe is a working program do I get a runtime error from
the judge?
My code:
#include <stdio.h>
#define MAXN 2999
#define MAXDIFF 2999
int takenon[MAXDIFF];
/* read: read n numbers into a */
void read(int n, int a[])
{
int i;
for (i = 0; i < n; i++)
scanf("%d", a + i);
}
int absdiff(int a, int b)
{
return a>b ? a-b : b-a;
}
int isjolly(int n, int a[])
{
int i;
for (i = 0; i < n-1; i++)
takenon[absdiff(a[i], a[i+1])] = 1;
for (i = 1; i <= n-1 && takenon[i]; i++)
;
return i == n ? 1 : 0;
}
int main()
{
int n, num[MAXN];
int i;
while (scanf("%d", &n) == 1) {
if (n <= 0)
puts("Not jolly");
else {
Which is why the OP had the next line...
> #define MAXDIFF 2999
Which doesn't alter the fact that the OP only
allocates storage for 2999 input integers in
the subsequent code, despite the specs saying
there may be up to 3000 such integers.
--
Peter
> Ben Bacarisse wrote:
<snip>
>> You will be better off writing:
>>
>> while (scanf("%d", &n) == 1) {...}
>>
>> <snip>
>
> Alright: but why after redirecting an input file
> 0
> 4 1 4 2 3
> 5 1 4 2 -1 6
> 1 1052
> to what I believe is a working program do I get a runtime error from
> the judge?
I don't think your code misbehaves on this data. I suspect the judge
is providing other data that does cause a problem misbehave. See
Peter's comments about your array size.
BTW:
<snip>
> while (scanf("%d", &n) == 1) {
> if (n <= 0)
> puts("Not jolly");
This special case is not needed.
> else {
> read(n, num);
> if (isjolly(n, num))
> puts("Jolly");
> else
> puts("Not jolly");
> }
> }
> return 0;
--
Ben.
You're right; I should have read the OP's code more carefully before posting.