UVa Problem 10038 Jolly Jumpers (快乐的跳跃者)

本文介绍了一种名为“快乐的跳跃者”(Jolly Jumpers)的问题解决方案,该问题要求检查一系列整数是否构成特定模式。通过分析相邻整数差值的分布来判断序列是否符合Jolly Jumpers定义。文章提供了完整的C++实现代码,包括输入处理、逻辑验证及输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// Jolly Jumpers (快乐的跳跃者) // PC/UVa IDs: 110201/10038, Popularity: A, Success rate: average Level: 1 // Verdict: Accepted // Submission Date: 2011-05-22 // UVa Run Time: 0.020s // // 版权所有(C)2011,邱秋。metaphysis # yeah dot net // // 检查相邻两个数的差的绝对值是否在 1 ~(N - 1) 的范围并且只出现一次。需要注意的是序列:1,也是 // Jolly Jumpers。如果序列全为负数,也可能是一个 Jolly Jumpers,如:4 -8 -5 -7 -6,也 // 构成一个 Jolly Jumpers。 #include <iostream> #include <cstring> #include <cstdlib> #include <sstream> using namespace std; #define MAXSIZE (3000 + 1) int main(int ac, char *av[]) { bool appeared[MAXSIZE]; int capacity, total, first, second, tmp; bool flag; string line; while (getline(cin, line)) { istringstream iss(line); iss >> capacity; total = capacity; flag = true; if (capacity > 1) { // 将标志某数是否已出现的数组全部置 0。 memset(appeared, false, sizeof(appeared)); // 读入前两个数。 iss >> first >> second; capacity -= 2; tmp = abs(second - first); if (tmp > (total - 1) || tmp == 0) { flag = false; goto out; } appeared[tmp] = true; // 如果还有数,继续读入。 while (capacity) { first = second; iss >> second; capacity--; // 若差的绝对值大于(N - 1)或者等于零,则肯定不是 Jolly Jumpers。 tmp = abs(second - first); if (tmp > (total - 1) || tmp == 0) { flag = false; goto out; } // 同样的差值只能出现 1 次。 else if (appeared[tmp]) { flag = false; goto out; } else appeared[tmp] = true; } // 判断从 1 ~ (N - 1)的差值是否都出现。 for (int i = 1; i < total; i++) if (appeared[i] == false) { flag = false; break; } } else { if (capacity == 1) { iss >> first; flag = (first == 1); } else flag = false; } out: cout << (flag ? "Jolly" : "Not jolly") << endl; } return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值