Detecting an empty line in stdin

A

Albert

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

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 = 0;

for (i = 0; i < n-1; i++)
takenon[absdiff(a, a[i+1])] = 1;
for (i = 1; i <= n-1 && takenon; 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
 
C

Chris McDonald

Albert said:
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.
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 
B

Ben Bacarisse

Albert said:
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.
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?

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.
 
P

Peter Nilsson

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.

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 = 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, a[i+1])] = 1;


What if the absolute difference is not in the range 0..2999?
                for (i = 1; i <= n-1 && takenon; 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) != 1)
return 0;

for (i = 0; i < MAX_D + 1; i++)
d = 0;

for (i = 0; i < N - 1; i++)
d[ diff(n, n[i + 1]) ] = 1;

jolly = (d[0] == 0);
for (i = 1; i <= N - 1; i++)
jolly = (jolly && d == 1);

puts(jolly ? "Jolly" : "Not jolly");
}

return 0;
}
 
A

Albert

Ben said:
Albert said:
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.
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?

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) {...}

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

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, a[i+1])] = 1;
for (i = 1; i <= n-1 && takenon; 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 {
read(n, num);
if (isjolly(n, num))
puts("Jolly");
else
puts("Not jolly");
}
}
return 0;
}
 
P

Peter Nilsson

[email protected] (Ike Naar) said:
3000 numbers have 2999 differences.

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.
 
B

Ben Bacarisse

Albert said:
Ben Bacarisse wrote:

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:
while (scanf("%d", &n) == 1) {
if (n <= 0)
puts("Not jolly");

This special case is not needed.
 
I

Ike Naar

Which is why the OP had the next line...


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.

You're right; I should have read the OP's code more carefully before posting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top