当前位置: 代码迷 >> 综合 >> ZCMU--1857: Problem C: Jolly Jumpers(C语言)
  详细解决方案

ZCMU--1857: Problem C: Jolly Jumpers(C语言)

热度:51   发布时间:2023-12-06 10:09:26.0

题目描述

A sequence of n > 0 integers is called a jolly jumper if the absolute values of the difference between successive elements take on all the values 1 through n-1. For instance,

1 4 2 3

is a jolly jumper, because the absolutes differences are 3, 2, and 1 respectively. The definition implies that any sequence of a single integer is a jolly jumper. You are to write a program to determine whether or not each of a number of sequences is a jolly jumper.

Each line of input contains an integer n < 3000 followed by n integers representing the sequence. For each line of input, generate a line of output saying "Jolly" or "Not jolly".

样例输入

4  1  4  2  3

5 1 4 2 -1 6

样例输出

Jolly

Not jolly

解析:题目就是问输入的N个数,两两相邻的差值,最后是否1到N-1每个数都有。所以我们可以直接利用数组来解决。

#include <stdio.h>
#include <math.h>
int k[3000];//存储输入的数 
int h[10005];//存储差值,差值为2就存入k【2】,如此 
int main()
{int n,i,shi;while(~scanf("%d",&n)){for(i=0;i<10005;i++) h[i]=0; //清空一下 for(i=0;i<n;i++) scanf("%d",&k[i]);for(i=0;i<n-1;i++) h[abs(k[i]-k[i+1])]++; //注意绝对值 shi=1;//shi=1代表该值存在 for(i=1;i<=n-1;i++){if(h[i]==0){ //等于0就是说不存在当前i的数,就不满足题意 shi=0;break;}}if(shi==0) printf("Not jolly\n");if(shi==1) printf("Jolly\n");}return 0;
}

  相关解决方案