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

// 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;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值