原题入口
题目描述
Jolly Jumpers
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17683 Accepted: 5348
Description
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”.
Sample Input
4 1 4 2 3
5 1 4 2 -1 6
Sample Output
Jolly
Not jolly
Source
Waterloo local 2000.09.30
解题代码
#include <iostream>
#include <cmath>
using namespace std;
#define SEQUENCE_COUNT 3000
char flag[SEQUENCE_COUNT] = {0};
int main()
{
int N; // 序列中数字的个数
int nNum, nNextNum; // 连续的两个数字
while ((cin >> N) && N)
{
cin >> nNum;
if (N == 1)
{
cout << "Jolly" << endl;
continue;
}
memset(flag, 0, sizeof(flag));
for (int nIndex = 2; nIndex <= N; ++nIndex)
{
cin >> nNextNum;
int nDiff = abs(nNextNum - nNum);
if (nDiff < SEQUENCE_COUNT)
flag[nDiff] = 1;
nNum = nNextNum;
}
int nFlagIndex = 1;
for (; nFlagIndex < N; ++nFlagIndex)
{
if (flag[nFlagIndex] <= 0)
{
cout << "Not jolly" << endl;
break;
}
}
if (nFlagIndex == N)
cout << "Jolly" << endl;
}
return 0;
}
解题过程
题意:每行开头给你一个正整数n,后面紧跟n个数组成的序列,序列中连续两个数字作差求绝对值,如果所有的绝对值占满了[1,n-1]之间的所有数字,则输出”Jolly”,否则输出”Not jolly”,值的注意的是,根据定义,任何一个单个的数字都是”Jolly Jumpers”。
思路:做一个标记数组就解决战斗乐,作差的过程中,将绝对值对应的数组位置设置成1,一行结束后遍历数组即可得出结果
槽点:这个问题得好好说一下,通过率才30%,我怀着忐忑的心情一遍水过,下面说说为啥会忐忑:
1.注意题目中的一个单词successive,这个词居然达到了专家级难度,它的意思是连续的,也就是作差需要相邻的两个数来做。
2.The definition implies that any sequence of a single integer is a jolly jumper.这一句需要注意到,否则WA。
3.说到n==1时输出”Jolly”,有人却说这样写会报错,不写的话通过了,我看到这都凌乱了,我只想说我是这样写的才通过,那种不考虑n==1的情况我没试,不想降低概率。
4.还有一点需要注意的是,n==1时后面还有一个数字别忘了读,否则就出错了。
5.就是因为发现这些坑和这么低的AC率我才忐忑的提交,结果一遍过了。。。