您的位置:首页 > 其它

POJ2575 ZOJ1879 UVA10038 Jolly Jumpers【序列】

2018-01-24 08:02 316 查看
Jolly Jumpers

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17815 Accepted: 5383
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 3
5 1 4 2 -1 6

Sample Output
Jolly
Not jolly

Source
Waterloo local 2000.09.30

问题链接POJ2575 ZOJ1879 UVA10038 Jolly Jumpers

问题简述

  每行输入n(n<=300)和n个整数,如果相邻的两个数之差正好是1到n-1之间的所有数,则输出“Jolly”,否则输出“Not jolly”。

问题分析
  需要标记数组diff[],标记已经出现的差值是否出现。其他都是套路。

程序说明:(略)

题记:(略)

参考链接:(略)

AC的C++语言程序如下:

/* POJ2575 ZOJ1879 UVA10038 Jolly Jumpers */

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

using namespace std;

const int N = 3000;
int a
;
bool diff
;

bool check(int len)
{
memset(diff, false, sizeof(diff[0]) * len);

for(int i=1; i<len; i++) {
int d = abs(a[i] - a[i - 1]);

if(d > len - 1 || d == 0 || diff[d])
return false;

diff[d] = true;
}

return true;
}

int main()
{
int n;

while(~scanf("%d", &n)) {
for(int i=0; i<n; i++)
scanf("%d", &a[i]);

printf("%s\n", check(n) ? "Jolly" : "Not jolly");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: