• SDNU 1227.A great challenge.(埃氏筛)


    Description

    In the legend, there lived the most intelligent person named Haroro in SDNU. Haroro likes to challenge other people on the prime problem. She is an unbeaten myth. Now, turn to you. If you can find out the most close to its prime number faster than Haroro, you will receive an unexpected gift.
    eg: If the screen appears 22, your answer should be 23;If the screen appears 8, your answer should be 7; if the X itself is prime, your answer should be X;if the nearest X prime number is two, then the answer is greater than its prime.

    Input

    The first line of the input contains the single number T(1 ≤ T≤ 100). Each of next T lines contains one number X(1≤X≤1000).

    Output

    The output contains N line, your answer should be the most close to its prime.
     

    Sample Input

    3
    22
    7
    8

    Sample Output

    23
    7
    7

    Hint

     

    Source

    Unknown
    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    const int inf = 0x3f3f3f3f;
    const int maxn = 1000 + 8;
    
    int t, n, tot, pri[maxn];
    bool is[maxn];
    
    void e()
    {
        tot = 0;
        memset(is, 1, sizeof(is));
        is[0] = is[1] = 0;
        for(int i = 2; i < maxn; i++)
        {
            if(is[i])
            {
                pri[++tot] = i;
                for(int j = i + i; j <= maxn; j += i)
                    is[j] = 0;
            }
        }
    }
    
    int main()
    {
    //    std::ios::sync_with_stdio(0);
    //    cin.tie(0);
    //    cout.tie(0);
        e();
        scanf("%d", &t);
        for(int i = 0; i < t; i++)
        {
            scanf("%d", &n);
            int m = upper_bound(pri + 1, pri + tot + 1, n) - pri;
            if(abs(pri[m] - n) > abs(pri[m - 1] - n))
                printf("%d
    ", pri[m - 1]);
            else
                printf("%d
    ", pri[m]);
        }
        return 0;
    }
  • 相关阅读:
    1203—颜文生—自动机实验
    11-11 优点缺点评价
    操作系统之银行家算法避免死锁
    操作系统之实验三 进程调度模拟程序
    操作系统之实验二作业调度模拟程序
    操作系统之实验二Step1-有序顺序表
    复利计算程序的单元测试
    操作系统之实验一 命令解释程序的编写
    复利计算程序之最新版
    《构建之法》第1.2.3章的感悟
  • 原文地址:https://www.cnblogs.com/RootVount/p/11536495.html
Copyright © 2020-2023  润新知