2018年1月12日 星期五

[MM22-易] Jolly Jumpers(C++)

[MM22-易] Jolly Jumpers

Problem DescriptionA 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 File Format
Each line of input contains an integer n <= 3000 followed by n integers representing the sequence.
Output Format
For each line of input, generate a line of output saying "Jolly" or "Not jolly".
Example

Sample Input:Sample Output:
4 1 4 2 3
5 1 4 2 -1 6
Jolly
Not jolly
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<cstdlib>  
  4.    
  5. using namespace std;  
  6. int main(){  
  7.     int n;  
  8.     int jolly[3005];  
  9.     bool jollyp = 1;  
  10.     while( scanf( "%d", &n ) != EOF )  
  11.     {  
  12.         bool jump[3005] = {0};  
  13.         jollyp = 1;  
  14.         forint i = 0 ; i < n ; i++ )  
  15.         {  
  16.             scanf( "%d", &jolly[i] );  
  17.         }  
  18.         forint i = 1 ; i < n ; i++ )  
  19.         {  
  20.             if( abs(jolly[i] - jolly[i-1]) >= n ||  
  21.                 abs(jolly[i] - jolly[i-1]) <= 0 ||   
  22.                 jump[abs(jolly[i] - jolly[i-1])] )  
  23.             {  
  24.                 jollyp = 0;  
  25.                 break;  
  26.             }  
  27.             jump[abs(jolly[i] - jolly[i-1])] = 1;  
  28.         }  
  29.         if( jollyp )  
  30.             printf( "Jolly\n" );  
  31.         else  
  32.             printf( "Not jolly\n" );  
  33.     }  
  34.     return 0;  
  35. }  

沒有留言:

張貼留言