题目描述
一个n个元素的整数数组,如果数组两个连续元素之间差的绝对值包括了[1,n−1]之间的所有整数,则称之符合“欢乐的跳”,如数组14231 符合“欢乐的跳”,因为差的绝对值分别为:3,2,1。
给定一个数组,你的任务是判断该数组是否符合“欢乐的跳”。
输入格式
每组测试数据第一行以一个整数n(1≤n≤1000)开始,接下来n个空格隔开的在[-10^8 , 10 ^8]之间的整数。
输出格式
对于每组测试数据,输出一行若该数组符合“欢乐的跳”则输出"Jolly",否则输出"Not jolly"。
输入输出样例
输入 4 1 4 2 3
输出 Jolly
输入5 1 4 2 -1 6
输出 Not jolly
说明/提示
1≤n≤1000
#include<bits/stdc++.h>
using namespace std;
int n;
int a[1005];
bool b[1005];
void cmp(){ //统计
for(int i=0;i<n-1;i++){
int m = a[i] - a[i+1];
if(m<0) m=-m;
if(1<=m && m <=n-1){
b[m]=1;
}
}
}
bool isGood(){
for(int i=1;i<=n-1;i++){
if(b[i]==0){
return false;
}
}
return true;
}
int main(){
ios::sync_with_stdio(0);
cin>>n;
for(int i = 0;i<n;i++){
cin>>a[i];
}
cmp();
if(isGood()) {
cout<<"Jolly";
}else{
cout<<"Not jolly";
}
return 0;
}