对于一个包含n>0个元素的整数序列,如果序列中相邻元素之差的绝对值取遍从1到n-1的所有整数,那么这个序列就叫做jolly number。例如1 4 2 3就是一个jolly number,因为相邻元素之差的绝对值分别为3、2、1。这个定义意味着所有的单元素序列都是jolly number.写一个程序来判断一个序列是不是jolly jumber.输入:输入的每行首先包含一个整数n(表示序列的长度,n<=3000),然后紧跟着n个整数,表示一个输入序列。输出;对于输入的每一行,输出一行“Jolly”或者”Not”jolly”来表示它是否为jolly number样例输入 样例输出4 1 4 2 3 Jolly5 1 4 2 -1 6 Not jolly
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int main() { int n, i, pre, cur; char hash[3000]; while(scanf("%d", &n) == 1) { memset(hash, 0, sizeof(hash)); scanf("%d",&pre); for(i = 0; i < n - 1; i++) { scanf("%d",&cur); hash[abs(pre - cur)] = 1; pre = cur; } for(i = 1; i < n ; i++)//1 到 n - 1 !!! { if(hash[i] == 0) { printf("Not jolly\n"); break; } } if(i == n) printf("Jolly\n"); } return 0; }
http://acm.zjut.edu.cn/Submit.aspx?ShowID=1004
RunID | User | Problem | Result | Time(MS) | Memory(K) | Length | Language | Submit Time |
---|---|---|---|---|---|---|---|---|
1031908 | wwjyt | 1004 | Accepted | 16 | 192 | 522 | GCC | 2013-2-26 5:58:47 |