题目描述
一个nnn个元素的整数数组,如果数组两个连续元素之间差的绝对值包括了[1,n−1][1,n-1][1,n−1]之间的所有整数,则称之符合“欢乐的跳”,如数组14231 4 2 31423符合“欢乐的跳”,因为差的绝对值分别为:3,2,13,2,13,2,1。
给定一个数组,你的任务是判断该数组是否符合“欢乐的跳”。
输入格式
每组测试数据第一行以一个整数n(1≤n≤1000)n(1 \le n \le 1000)n(1≤n≤1000)开始,接下来nnn个空格隔开的在[−108-10^8−108,10810^8108]之间的整数。
输出格式
对于每组测试数据,输出一行若该数组符合“欢乐的跳”则输出"Jolly",否则输出"Not jolly"。
输入输出样例
输入
4 1 4 2 3
输出
Jolly
输入
5 1 4 2 -1 6
输出
Not jolly
说明/提示
1≤n≤10001 \le n \le 10001≤n≤1000
import java.util.*;
public class P1152 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] num = new int[n];
for (int i = 0; i < n; i++) {
num[i] = sc.nextInt();
}
ArrayList list = new ArrayList();
for (int i = 0; i < n-1; i++) {
for (int j = i + 1; j <= i+1; j++) {
int tmp = num[i] - num[j];
list.add(Math.abs(tmp));
}
}
Set<Integer> set = new HashSet<>(list);
list.clear();
list.addAll(set);
ArrayList arrayList = new ArrayList();
for (int i = 1; i <= n - 1; i++) {
arrayList.add(i);
}
Collections.sort(list);
if (list.get(0).equals(0)) {
list.remove(0);
}
int count = 0;
if (list.size() > arrayList.size()||list.size()==arrayList.size()) {
for (int i = 0; i < arrayList.size(); i++) {
if (list.get(i).equals(arrayList.get(i)))
count++;
}
if (count == arrayList.size()) {
System.out.print("Jolly");
}
} else {
System.out.print("Not jolly");
}
}
}