10038-jolly Jumpers
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=979
http://poj.org/problem?id=2575
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1879
A sequence of n > 0 integers is called a jolly jumper if the absolute values of the difference between successive NTS 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 are to write a program to determine whether or not all of a number of sequences is a jolly jumper.
Input
Each line of input contains a 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
Note that there are two conditions that can exit the loop prematurely.
Complete code:
/*uva:0.012s*/
/*poj:16ms,184kb*/
/*zoj:0ms,204kb*/
#include <cstdio>
#include <cstring >
#include <cstdlib>
int arr[3010], vis[3010];
int main (void)
{
int n, i;
while (~SCANF ("%d", &n))
{
memset (vis, 0, sizeof (VIS));
for (i = 0; i < n; i++)
scanf ("%d", &arr[i]);
BOOL flag = TRUE;
for (i = 1; i < n; i++)
{
if (ABS (Arr[i]-arr[i-1]) >= N)
{
flag = false;
break;
if (Vis[abs (Arr[i]-arr[i-1])]++///Tips ~
{
flag = false;
break;
}
}
Puts (flag?) "Jolly": "Not Jolly");
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/