Description
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 3is 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 6Sample Output
Jolly Not jolly
难懂的题意。。(被题意坑了一发QAQ)
首先输入一个正整数n,后接n个数字,每连续的两个数字相减的绝对值组成1~n-1共n-1个数字。
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<cstdlib> 7 using namespace std; 8 9 const int MAX=3050; 10 int a[MAX]; 11 int b[MAX]; 12 int n; 13 int main(){ 14 while(~scanf("%d",&n)){ 15 for(int i=0;i<n;i++){ 16 scanf("%d",&a[i]); 17 } 18 if(n==1){//当n为1时直接输出 19 printf("Jolly "); 20 continue; 21 } 22 for(int i=1;i<n;i++){//两两相减的绝对值 23 b[i]=abs(a[i]-a[i-1]); 24 } 25 int temp; 26 for(int i=1;i<n;i++){//冒泡排序 27 for(int j=n-1;j>=i;j--){ 28 if(b[j]<b[j-1]){ 29 temp=b[j]; 30 b[j]=b[j-1]; 31 b[j-1]=temp; 32 } 33 } 34 } 35 int ans=0; 36 for(int i=1;i<n;i++){//记录相等数字数 37 if(b[i]==i) 38 ans++; 39 } 40 if(ans==n-1) 41 printf("Jolly "); 42 else 43 printf("Not jolly "); 44 } 45 return 0; 46 }