// 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;}elseappeared[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);}elseflag = false;}out:cout << (flag ? "Jolly" : "Not jolly") << endl;}return 0;}