uva 10038 Jolly Jumpers
November 5, 2012
Uva
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, 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. Each line of input contains an integer n <= 3000 followed by n integers representing the sequence. For each line of input, generate a line of output saying “Jolly” or “Not jolly”.
拙劣的代码
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <iomanip>
#define max 3000+5
using namespace std;
int main(void)
{
int n, a[max], i;
while (cin >> n)
{
int x, y;
cin >> x;
for (i = 0; i < n - 1; i++)
{
cin >> y;
a[i] = fabs((x) - (y));
x = y;
}
sort(a, a + n - 1);
int flag = 0;
for (i = 0; i < n - 1; i++)
{
if (a[i] != i + 1)
{
cout << "Not jolly\n";
flag = 1;
break;
}
}
if (!flag)
{
cout << "Jolly\n";
}
}
return 0;
}