【CodeForces】[653A]Bear and Three Balls

这里写图片描述

因为有
1 2 2 3
这种数据
所以不能简单的进行排序查找
可以先去除重复的数字

#include<stdio.h>
#include<algorithm>
using namespace std;
int a[55];
bool flag[55];
int n;
bool find() {
    sort(a,a+n);
    for(int i=0; i<n-2; i++)
        if(a[i]+1==a[i+1]&&a[i+1]+1==a[i+2])
            return true;
    return false;
}
int main() {
    while(scanf("%d",&n)!=EOF) {
        for(int i=0; i<n; i++)
            flag[i]=false;
        for(int i=0; i<n; i++) {
            scanf("%d",&a[i]);
            if(flag[a[i]]) {
                i--;
                n--;
            } else
                flag[a[i]]=true;
        }
        if(find())
            printf("YES
");
        else
            printf("NO
");
    }
    return 0;
}

题目地址:【CodeForces】[653A]Bear and Three Balls

原文地址:https://www.cnblogs.com/BoilTask/p/12569594.html