思路
-
计算相邻元素差值的绝对值:遍历数组,计算每两个相邻元素的差的绝对值
-
检查覆盖范围:使用一个布尔数组标记1到n-1范围内的数字是否出现
-
验证欢乐跳条件:检查1到n-1范围内的每个整数是否都出现在差值中
-
输出结果:如果所有数字都出现则输出"Jolly",否则输出"Not jolly"
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> nums(n);
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
if (n == 1) {
cout << "Jolly" << endl;
return 0;
}
vector<bool> exists(n, false);
for (int i = 1; i < n; i++) {
long long diff = abs(nums[i] - nums[i-1]);
if (diff >= 1 && diff <= n-1) {
exists[diff] = true;
}
}
bool isJolly = true;
for (int i = 1; i < n; i++) {
if (!exists[i]) {
isJolly = false;
break;
}
}
cout << (isJolly ? "Jolly" : "Not jolly") << endl;
return 0;
}