Description
Problem C: 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.
Each line of input contains an integer n < 3000 followed by n integers representing the sequence. For each line of input, generate a line of output saying "Jolly" or "Not jolly".
Input
Output
Sample Input
4 1 4 2 3
5 1 4 2 -1 6
Sample Output
Jolly
Not jolly
解析:
我一开始以为必须按照n-1~1的顺序来,后来提交上去是错的,发现只要是1~n-1全部出现一次就可以了,水题~没其它要解析的。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(cin>>n){
int a[n]; //用来储存连续元素
int b[n]; //确认n是否已经是某两个连续元素之间的差
memset(b,0,sizeof(b)); //初始化1~n-1都没有被使用过
int flag=0,k;
for(int i=0;i<n;i++){
if(i==0) cin>>a[i];
else{
cin>>a[i];
k=a[i]-a[i-1];
if(k<0) k=-k;
if(k>0&&k<n&&b[k]==0) b[k]=1; //k这个差已经被使用
else flag=1; //k这个差被使用过之后不能再次被使用
}
}
if(flag==0) cout<<"Jolly"<<endl;
else cout<<"Not jolly"<<endl;
}
return 0;
}
第一个博客,(●ˇ∀ˇ●)