Blue Bridge Cup-Prime number

Description
Title description:
Entering an information and communication pavilion in the Expo Park, visitors will have an unprecedented cutting-edge interactive experience. An information and communication interactive experience show full of creativity and joy will be presented in a new form. From the first step, it will be inseparable from the handheld terminal, and the surprise of the future dream of mankind will unfold from the palm of the visitor.

In the dream garden in the waiting area, the visitors began their wonderful experience journey. The waiting tourists can use mobile phones and other terminals to participate in interactive mini-games, and have a guessing contest with the virtual character Kr. Kong in the dream theater. When an integer X appears on the screen, if you can send out the closest prime answer faster than Kr. Kong, you will get an unexpected gift.

For example: when 22 appears on the screen, your answer should be 23; when 8 appears on the screen, your answer should be 7; if X itself is a prime number, answer X; if there are two prime numbers closest to X, then Answer a prime number greater than it.

Input:
The first line: N The number of integers to be guessed Next, there are N lines, each with a positive integer X 1<=N<=5 1<=X<=1000

Output:
The output has N lines, each line is the closest prime number corresponding to X

Sample Input
4
22
5
18
8
Sample Output
23
5
19
7

#include<iostream>
#include<cmath>
using namespace std;
bool ok = false;
bool f(int x)
{
    int k,i;
    k = (int)sqrt(x);
    for(i = 2;i <= k;i++)
        if(x % i == 0)
            break;
    if(i > k)
        return true;
    else
        return false;
}

int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        int x;
        cin >> x;
        for(int i = 0;;i++)
        {
            if(f(x + i) == 1)//查看数字的右边
            {
                cout << x + i << endl;
                break;
            }
            if(f(x - i) == 1)//查看数字的左边
            {
                cout << x - i << endl;
                break;
            }
        }
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325805254&siteId=291194637