UVa 10038 - Jolly Jumpers Solution
Must watch: More UVa Problem solution
Must watch: More UVa Problem solution
Description:
10038 - Jolly Jumpers
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’.Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #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; } |
Keywords: jolly jumpers uva solution, jolly jumper program in c,uva 10127, 10038 - jolly jumpers, jolly jumper sequence, uva 10038, uva10055, 3n+1 problem solution
إرسال تعليق