P1152 欢乐的跳

题目传送门

一、原始方法

#include<bits/stdc++.h>

using namespace std;
const int N = 1010;
int a[N], c[N];

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);//读入
    for (int i = 1; i < n; i++) c[i] = abs(a[i] - a[i + 1]);//处理差
    //按差值由小到大排序
    sort(c + 1, c + n);
    //检查一下缺少哪个数字,缺少的条件是c[i]==i
    for (int i = 1; i < n; i++) {
        if (c[i] != i) {
            printf("Not jolly");
            return 0;
        }
    }
    printf("Jolly");
    return 0;
}

二、进化方法

#include<bits/stdc++.h>

using namespace std;
const int N = 1010;
int a[N];

unordered_set<int> s;

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];

    for (int i = 1; i < n; i++) {
        int x = abs(a[i] - a[i - 1]);
        s.insert(x);
    }

    for (int i = 1; i <= n - 1; i++) {
        //如果发现缺失,就不是欢乐的跳
        if (s.count(i) == 0) {
            cout << "Not jolly" << endl;
            exit(0);
        }
        //如果成功到达最后,就是欢乐的跳
        if (i == n - 1) cout << "Jolly" << endl;
    }
    return 0;
}
posted @ 2021-11-23 14:50  糖豆爸爸  阅读(160)  评论(0编辑  收藏  举报
Live2D