2019 蓝桥杯省赛 B 组模拟赛(一) F. 程序设计:找质数

一天蒜头君猜想,是不是所有的偶数(除了 222),都可以用两个质数相加得到呢?于是聪明的蒜头君就找你来验证了。

输入格式

第一行输入一个整数 ttt 表示测试组数。

接下来 ttt 行,每行一个整数 nnn。

输出格式

输出两个整数,因为答案可能有多个,所有要求输出的这两个整数是所有答案中字典序最小的。

数据范围

对于 30%30\%30% 的数据 1≤t≤1031 \le t \le 10^31≤t≤103。

对于 60%60\%60% 的数据 1≤t≤1051 \le t \le 10^51≤t≤105。

对于 100%100\%100% 的数据 1≤t≤106,4≤n≤1061 \le t \le 10^6, 4 \le n \le 10^61≤t≤106,4≤n≤106,nnn 为偶数。

样例输入

3
4
8
20

样例输出

2 2
3 5
3 17

思路:

先把素数都找出来。

然后从最小的挨个枚举,看看另一个是否为素数。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
int vis[maxn];
int cnt,su[maxn];
int t,n;
void solve()
{
    for (int i=2;i<=maxn-5;i++)
    {
        if(!vis[i])
        {
            su[cnt++]=i;
            for (int j=2*i;j<=maxn-5;j+=i)
            {
                vis[j]=1;
            }
        }
    }
}
int main()
{
    memset (vis,0,sizeof(vis));
    solve();
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d",&n);
        for (int i=0;i<cnt;i++)
        {
            if(!vis[n-su[i]])
            {
                printf("%d %d\n",su[i],n-su[i]);
                break;
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/87381156
今日推荐