博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2575 Jolly Jumpers(我的水题之路——数组绝对差值为1到n-1)
阅读量:4069 次
发布时间:2019-05-25

本文共 1935 字,大约阅读时间需要 6 分钟。

Jolly Jumpers
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13365   Accepted: 4128

Description

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.

Input

Each line of input contains an integer n < 3000 followed by n integers representing the sequence.

Output

For each line of input, generate a line of output saying "Jolly" or "Not jolly". 

Sample Input

4 1 4 2 35 1 4 2 -1 6

Sample Output

JollyNot jolly

Source

对于输入的n个数字,它们的相邻两个元素之间取绝对差值,如果这些差值为1到n-1(无论次序),就输出“Jolly”,否则输出“Not jolly”。
用一个数组subs作为标记数组,标记已经出现过差值,当出现一个差值就标记为1。当所有的差值计算完毕之后,搜索1到n-1的差值是否全部出现,如果是,
输出“Jolly”,否则输出“Not jolly”。优化:因为是n个数字,有n-1个差值,如果这些差值要取满1到n-1,则说明这些差值之间不会有重复的数字,所以在计算差值的过程中就可以进行判断,如果有重复的差值,则立刻判定“Not jolly”。
注意点:
1)计算差值的时候,要取绝对值,可以使用<cmath>里的abs();
代码(1AC):
#include 
#include
#include
#include
int subs[3100];int num[3100];int main(void){ int n, i, j; int flag; while (scanf("%d", &n) != EOF){ memset(subs, 0, sizeof(subs)); flag = 1; for (i = 0; i < n; i++){ scanf("%d", &num[i]); if (i){ if (!subs[abs(num[i] - num[i - 1])]){ subs[abs(num[i] - num[i - 1])] = 1; } else{ flag = 0; } } } for (i = 1; i < n && flag; i++){ if (subs[i] != 1){ flag = 0; } } if (flag){ printf("Jolly\n"); } else{ printf("Not jolly\n"); } } return 0;}

转载地址:http://oooji.baihongyu.com/

你可能感兴趣的文章
PostgreSQL代码分析,查询优化部分,process_duplicate_ors
查看>>
PostgreSQL代码分析,查询优化部分,canonicalize_qual
查看>>
PostgreSQL代码分析,查询优化部分,pull_ands()和pull_ors()
查看>>
ORACLE权限管理调研笔记
查看>>
移进规约冲突一例
查看>>
IA32时钟周期的一些内容
查看>>
SM2椭圆曲线公钥密码算法
查看>>
获得github工程中的一个文件夹的方法
查看>>
《PostgreSQL技术内幕:查询优化深度探索》养成记
查看>>
PostgreSQL查询优化器详解之逻辑优化篇
查看>>
STM32中assert_param的使用
查看>>
C语言中的 (void*)0 与 (void)0
查看>>
vu 是什么
查看>>
io口的作用
查看>>
IO口的作用
查看>>
UIView的使用setNeedsDisplay
查看>>
归档与解归档
查看>>
Window
查看>>
为什么button在设置标题时要用一个方法,而不像lable一样直接用一个属性
查看>>
字符串的截取
查看>>