UVa10038 Jolly Jumpers

有n個整數的序列我們稱為jolly jumper,如果相鄰的2個數其差的絕對值恰好為1到n-1。
例如:1 4 2 3 就是jolly jumper(n=4)。因為相鄰2數的差的絕對值為3,2,1,就是1到n-1。
但是 1 4 2 -1 6 不是jolly jumper(n=5)。因為相鄰2數的差的絕對值為3,2,3,7,並非1到n-1。

你的任務是寫一個程式來判斷一個整數序列是否為jolly jumper。

Input

每組測試資料一列,第一個正整數為 n(n <= 3000),代表此整數序列的長度。接下來有n個整數,代表此整數序列。請參考Sample Input。

Output

對每一組測試資料,輸出此整數序列是否為jolly jumper。請參考Sample Output。

Sample Input

4 1 4 2 3
5 1 4 2 -1 6

Sample Output

Jolly
Not jolly
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int n;
    while (~scanf("%d", &n)){
        int array[3001] = {};
        int str[3001] = {};

        for (int i = 0; i < n; i++)
            scanf("%d", &array[i]);

        for (int i = 0; i < n - 1; i++)
            str[abs(array[i] - array[i+1])] = true;

        int count = 1;
        for (int i = 1; i <= n - 1; i++)
            if (str[i]) count ++;

        if (count == n - 1) printf("Jolly\n");
        else printf("Not jolly\n");
    }
}

 

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注