POJ2575 ZOJ1879 UVA10038 Jolly Jumpers【序列】

509 篇文章 9 订阅
232 篇文章 0 订阅

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


问题链接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[N];
bool diff[N];

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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值