sicily1190. Reduced ID Numbers

来源:互联网 发布:java简单的小游戏 编辑:程序博客网 时间:2024/06/09 20:00

这道题目,个人认为比较水。只要利用合适的数据结构就可以很快的通过。个人算法能力不是很好,通过的时间为0.83s,题目要求的时间上限为2s。可以看出,我的以下这个算法并不是很好。代码仅供参考。若有更好的算法,欢迎私信我。共同学习,共同进步。。

#include <iostream>#include <stdio.h>#include <set>using namespace std;int main(){    int t;    scanf("%d", &t);    int a[309];    int n;    while (t --)    {        scanf("%d", &n);        for (int i = 0; i < n; ++ i)        {            scanf("%d", &a[i]);        }        int ant = n;        bool che;        while (1)        {            set<int> s;            che = true;            for (int i = 0; i < n; ++ i)            {                int temp = a[i] % ant;                if (s.count(temp))                {                    che = false;                    break;                }                else                {                    s.insert(temp);                }            }            if (che)            {                break;            }            else            {                ++ ant;            }        }        printf("%d\n", ant);    }}

原创粉丝点击