I've tried just about every sample input from the boards and they all seem to work properly in my program. Maybe someone can help?
Here's my code:
[cpp]
#include <iostream>
#include <algorithm>
using namespace std;
template < class T > T absoluteValue( T input )
{
if ( input < 0 )
{
input *= -1;
}
return input;
}
class Main
{
static int abs(int number)
{
if (number < 0)
{
return -1 * number;
}
else return number;
}
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
public static void main (String args[]) // entry point from OS
{
Main myWork = new Main(); // create a dinamic instance
myWork.Begin(); // the true entry point
}
void Begin()
{
int array[];
String input;
StringTokenizer idata;
int n,a,b,flag;
while ((input = Main.ReadLn (255)) != null)
{
flag = 0;
idata = new StringTokenizer (input);
n = Integer.parseInt (idata.nextToken());
if (n == 1)
{
System.out.println("Jolly");
continue;
}
array = new int[n - 1];
for(int loop = 0;loop < n - 1;loop++)
{
array[loop] = loop + 1;
}
a = Integer.parseInt (idata.nextToken());
b = Integer.parseInt (idata.nextToken());
if (abs(a - b) >= n)
{
System.out.println("Not jolly");
}
else
{
for(int loop2 = 0;loop2 < n - 1;loop2++)
if (array[loop2] == abs(a - b))
{
array[loop2] = 0;
break;
}
for(int loop=1;loop < n - 1;loop++)
{
a = b;
b = Integer.parseInt (idata.nextToken());
if (abs(a - b) >= n)
{
System.out.println("Not jolly");
flag = 1;
break;
}
else
{
for(int loop2 = 0;loop2 < n - 1;loop2++)
if (array[loop2] == abs(a - b))
{
array[loop2] = 0;
break;
}
}
}
if (flag == 0)
{
for(int loop = 0;loop < n - 1;loop++)
if(array[loop] != 0)
{
System.out.println("Not jolly");
flag = 1;
break;
}
if (flag == 0) System.out.println("Jolly");
}
}
}
}
}[/java]
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
I'm sorry my english is poor.I just make a array that contains the numbers from 1 to n so that i can compare with the absolutes differences
[java]
import java.io.*;
import java.util.*;
class Main
{
static int abs(int number)
{
if (number < 0)
{
return -1 * number;
}
else return number;
}
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
public static void main (String args[]) // entry point from OS
{
Main myWork = new Main(); // create a dinamic instance
myWork.Begin(); // the true entry point
}
void Begin()
{
int array[];
String input;
StringTokenizer idata;
int n,a,b,flag;
while ((input = Main.ReadLn (255)) != null)
{
flag = 0;
idata = new StringTokenizer (input);
n = Integer.parseInt (idata.nextToken());
if (n == 1)
{
//a single integer is always Jolly
System.out.println("Jolly");
continue;
}
array = new int[n - 1];
//i put the numbers from 1 to n in array so that i can compare with the absolutes differences
for(int loop = 0;loop < n - 1;loop++)
{
array[loop] = loop + 1;
}
a = Integer.parseInt (idata.nextToken());
b = Integer.parseInt (idata.nextToken());
//a and b are two absolutes differences one by one in input
if (abs(a - b) >= n)
{
System.out.println("Not jolly");
}
else
{
for(int loop2 = 0;loop2 < n - 1;loop2++)
if (array[loop2] == abs(a - b))
{
array[loop2] = 0;
break;
//give 0 to array[loop2] to mark this value has appeared in absolutes differences
}
for(int loop=1;loop < n - 1;loop++)
{
a = b;
b = Integer.parseInt (idata.nextToken());
//a and b are next two absolutes differences
if (abs(a - b) >= n)
{
System.out.println("Not jolly");
flag = 1;
//flag mark that it is definitely "Not jolly"
break;
}
else
{
for(int loop2 = 0;loop2 < n - 1;loop2++)
if (array[loop2] == abs(a - b))
{
array[loop2] = 0;
break;
//just like what i did above
}
}
}
if (flag == 0)//if whether it is "Not jolly" is not sure
{
for(int loop = 0;loop < n - 1;loop++)
if(array[loop] != 0)
{
//if any element in array not equals to 0,then it must be "Not jolly"
System.out.println("Not jolly");
flag = 1;
break;
}
if (flag == 0) System.out.println("Jolly");
}
}
}
}
}[/java][/java]
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
Sorry, I know java very and very bad. All that I can help you is my algo:
1) read input to array a[1..n] of integer
2) create other array b[1..n] of boolean
for i=1 to n-1 do
if |a[i] - a[i+1]|>=n then /Not jolly/
else b[|a[i] - a[i+1]|]:=true
for i=1 to n-1 if not b[i] then /Not jolly/
else {if b[i]=true for all i=1..n-1} /Jolly/
Sorry, I know java very and very bad. All that I can help you is my algo:
1) read input to array a[1..n] of integer
2) create other array b[1..n] of boolean
for i=1 to n-1 do
if |a[i] - a[i+1]|>=n then /Not jolly/
else b[|a[i] - a[i+1]|]:=true
for i=1 to n-1 if not b[i] then /Not jolly/
else {if b[i]=true for all i=1..n-1} /Jolly/
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
while(scanf("%d", &num) != EOF)
{
int stat[num];
int jolly = 1;
int val = 0;
char temp;
for(i=0; i<num; i++)
{
stat = 0;
}
if (num == 1)
{
printf("Jolly\n");
scanf("%d", &num); /* Consume extra number */
goto end; /* Break loop */
} else {
scanf("%d", &n2); /* First number into n2 */
for(i=1; i<num; i++)
{
n1 = n2; /* First number into n1 */
scanf("%d", &n2); /* Next number into n2 */
val = abs(n1-n2);
if (val < num) /* If val is in the bounds */
stat[abs(n1-n2)] = 1; /* Then set flag in array */
else /* Else print Not Jolly */
{ /* and consume rest of line */
printf("Not jolly\n");
scanf("%c", &temp);
while (temp != '\n')
scanf("%c", &temp);
goto end;
}
}
for(i=1; i<num; i++)
jolly = jolly && stat; /* Boolean with all values */
if (jolly == 1)
printf("Jolly\n");
else
printf("Not jolly\n");
All the numbers from 1 to 5 are seen at least once in the above list so the sequence is a jolly jumper. They don't have to be seen in any particular order.
EDIT: Read the problem statement carefully.
EDIT 2: Edited to make a clearer explanation.
Computer Science is no more about computers than Astronomy is about telescopes.
-- E. W. Dijkstra