Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detecting an empty line in stdin

2 views
Skip to first unread message

Albert

unread,
Jan 13, 2010, 2:30:16 AM1/13/10
to
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[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

Chris McDonald

unread,
Jan 13, 2010, 2:41:48 AM1/13/10
to
Albert <albert.xt...@gmail.com> writes:

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

Michael Tsang

unread,
Jan 13, 2010, 6:47:08 AM1/13/10
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

pete

unread,
Jan 13, 2010, 7:04:59 AM1/13/10
to
Albert wrote:

> 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

pete

unread,
Jan 13, 2010, 8:02:35 AM1/13/10
to

Here's an example with numeric input:

http://www.mindspring.com/~pfilandr/C/fscanf_input/grade.c

--
pete

Ben Bacarisse

unread,
Jan 13, 2010, 10:01:56 AM1/13/10
to
Albert <albert.xt...@gmail.com> writes:
<snip>

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

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

--
Ben.

Ben Bacarisse

unread,
Jan 13, 2010, 10:04:41 AM1/13/10
to
Michael Tsang <mik...@gmail.com> writes:

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

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=30&page=show_problem&problem=979

--
Ben.

Peter Nilsson

unread,
Jan 13, 2010, 5:28:23 PM1/13/10
to
On Jan 13, 6:30 pm, Albert <albert.xtheunkno...@gmail.com> 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.

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

Ike Naar

unread,
Jan 13, 2010, 5:53:27 PM1/13/10
to
In article <52aaaf8d-4bba-4f02...@e27g2000yqd.googlegroups.com>,

Peter Nilsson <ai...@acay.com.au> wrote:
>On Jan 13, 6:30�pm, Albert <albert.xtheunkno...@gmail.com> wrote:
>> #define MAXN 2999
>
>There may be 3000 integers!

3000 numbers have 2999 differences.

Albert

unread,
Jan 13, 2010, 11:01:48 PM1/13/10
to
> <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[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 {

Peter Nilsson

unread,
Jan 13, 2010, 11:02:57 PM1/13/10
to
i...@localhost.claranet.nl (Ike Naar) wrote:

> Peter Nilsson  <ai...@acay.com.au> wrote:
> > Albert <albert.xtheunkno...@gmail.com> wrote:
> > > #define MAXN 2999
> >
> > There may be 3000 integers!
>
> 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.

--
Peter

Ben Bacarisse

unread,
Jan 14, 2010, 6:32:06 AM1/14/10
to
Albert <albert.xt...@gmail.com> writes:

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

Ike Naar

unread,
Jan 14, 2010, 6:27:07 PM1/14/10
to
In article <c248de5e-7eb7-409a...@j4g2000yqe.googlegroups.com>,
Peter Nilsson <ai...@acay.com.au> wrote:

>i...@localhost.claranet.nl (Ike Naar) wrote:
>> 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.

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

0 new messages