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
这道题目注意一句话: if the absolute values of the difference between successive elements 意思是差的绝对值,不要理解错了,想这种句子要认真看.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <algorithm> 3 #include <cmath> 4 #include <cstdlib> 5 #include <cstdio> 6 #include <iomanip> 7 #define max 3000+5 8 9 using namespace std; 10 11 int main(void) 12 { 13 int n, a[max], i; 14 15 while (cin >> n) 16 { 17 int x, y; 18 cin >> x; 19 for (i = 0; i < n - 1; i++) 20 { 21 cin >> y; 22 a[i] = fabs((x) - (y)); 23 x = y; 24 } 25 sort(a, a + n - 1); 26 int flag = 0; 27 for (i = 0; i < n - 1; i++) 28 { 29 if (a[i] != i + 1) 30 { 31 cout << "Not jolly\n"; 32 flag = 1; 33 break; 34 } 35 } 36 if (!flag) 37 { 38 cout << "Jolly\n"; 39 } 40 } 41 42 return 0; 43 }