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


你可能感兴趣的:(c,Date)