UVa 10038 - Jolly Jumpers
Description:
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’.Solution:
#include<stdio.h> #include<stdlib.h> int main() { int n,arr[4000],item[4000],i,j,diff=0; while(scanf("%d",&n)==1) { for(i=1;i<=n;i++) arr[i]=0; for(i=1;i<=n;i++) scanf("%d",&item[i]); if(n!=1) { for(i=1;i<n;i++) { diff=abs(item[i]-item[i+1]); if(arr[diff]==0) arr[diff]=1; else { printf("Not jolly\n"); break; } if(i==(n-1)) { for(j=1;j<n;j++) { if(arr[j]==0) { printf("Not jolly\n"); break; } else if(j==(n-1)) printf("Jolly\n"); } } } } else printf("Jolly\n"); } return 0; }
See More Solution:
Comments
Post a Comment