有n個整數的序列我們稱為jolly jumper,如果相鄰的2個數其差的絕對值恰好為1到n-1。
例如:1 4 2 3 就是jolly jumper(n=4)。因為相鄰2數的差的絕對值為3,2,1,就是1到n-1。
但是 1 4 2 -1 6 不是jolly jumper(n=5)。因為相鄰2數的差的絕對值為3,2,3,7,並非1到n-1。
你的任務是寫一個程式來判斷一個整數序列是否為jolly jumper。
Input
每組測試資料一列,第一個正整數為 n(n <= 3000),代表此整數序列的長度。接下來有n個整數,代表此整數序列。請參考Sample Input。
Output
對每一組測試資料,輸出此整數序列是否為jolly jumper。請參考Sample Output。
Sample Input
4 1 4 2 3
5 1 4 2 -1 6
Sample Output
Jolly
Not jolly
#include <stdio.h> #include <stdlib.h> int main() { int n; while(scanf("%d",&n)!=EOF) { int arr[3001]={0}, str[3001]={0}, i; for(i=0; i<n; i++) scanf("%d",&arr[i]); for(i=0; i<n-1; i++) //将相邻两个数的差的绝对值放入阵列序列中并赋值1 str[abs(arr[i]-arr[i+1])]=1; int count=0; for(i=1; i<=n-1; i++) //计算在1到n-1这个范围内有几个1 if(str[i]==1) count++; if(count==n-1) printf("Jolly\n"); else printf("Not jolly\n"); } }