快乐的跳跃者 UESTC - 508
对于一个包含n(n>0)个元素的整数序列,如果序列中相邻元素之差的绝对值取遍从1到n−1的所有整数,那么这个序列就叫做jolly jumper。例如:1 4 2 3 就是一个jolly jumper,因为相邻元素之差的绝对值分别为3、2、1。写一个程序来判断一个序列是不是jolly jumper。
Input
有T组测试数据。输入的第一行是数据组数T,其后每一行是一组测试数据。每行包含一个整数n(n≤300),然后是n个整数,表示一个输入序列。
Output
对于输入的每一行,输出一行Jolly或者Not jolly来表示它是否为jolly jumper。
Sample Input
2
4 1 4 2 3
5 1 4 2 -1 6
Sample Output
Jolly
Not jolly
题意
略
题解
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(void)
{
int i,t,n,cnt;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int *a,*b;
a = (int *)malloc(sizeof(int) * n);
b = (int *)malloc(sizeof(int) * n - 1);
for(i = 0;i < n - 1; i++)
b[i] = i + 1;
for(i = 0;i < n; i++)
scanf("%d",&a[i]);
for(i = 0;i < n - 1; i++)
{
int x = abs(a[i] - a[i + 1]);
for(cnt = 0;cnt < n; cnt++)
{
if(b[cnt] == x)
{
b[cnt] = 0;
break;
}
}
}
int sum = 0;
for(i = 0;i < n - 1; i++)
{
if(b[i] == 0)
sum++;
}
if(sum == n - 1)
printf("Jolly\n");
else
printf("Not jolly\n");
}
return 0;
}