Uva10038 jolly Jumpers

Source: Internet
Author: User

Problem E: Jolly Jumpers A sequence of N> 0 integers is called a Jolly Jumper if the absolute values of the difference between successive elemtake 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 integerN<= 3000 followedNIntegers representing the sequence.

Outputfor 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
The absolute values of two adjacent numbers must be in [1, n), and each number in this range must appear once.

#include <stdio.h>#include <string.h>#include <stdlib.h>int vis[3002];int main(){int n, a, b, ok, t, m;while(scanf("%d", &n) != EOF){scanf("%d", &a);m = n; ok = 1;memset(vis, 0, sizeof(vis));while(--n){scanf("%d", &b);if(ok == 0) continue;if((t = abs(a - b)) == 0 || t >= m || vis[t])ok = 0; a = b; vis[t] = 1;}printf(ok ? "Jolly\n" : "Not jolly\n");}return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.