K11441 素数对。

K11441 素数对
题目描述
两个相差为2的素数称为素数对,如5和7,17和19等。
给定一个正整数n,请输出两个数均不大于n的所有素数对。
输入格式
一个正整数n(1≤n≤10000)
输出格式
所有小于等于n的素数对。每对素数对输出一行,中间用单个空格隔开。若没有找到任何素数对,输出empty。
输入输出样列
输入样例1:复制
100

输出样例1:复制
3 5
5 7
11 13
17 19
29 31
41 43
59 61
71 73

【耗时限制】1000ms 【内存限制】64MB

#include <iostream>
#include <cmath>
using namespace std;
int ss(int x)//判断x是不是素数,是返回1,不是返回0
{
    int f=1;
    for(int i=2;i<=sqrt(x);i++)
    {
        if(x%i==0)
        {
            f=0;
            break;
        }
    }
    return f;
}
main()
{
    int a,f=0;
    cin>>a;
    for(int i=3;i<=a-2;i=i+2)
    {
        if(ss(i)==1&&ss(i+2)==1)
        {
            f=1;
            cout<<i<<" "<<i+2<<endl;
        }
    }
    if(f==0)
    {
        cout<<"empty";
    }
}
#include <iostream>
#include <cmath>

using namespace std;

bool isprime(int x)
{
    if (x < 2)
        return false;
    int n = static_cast<int>(sqrt(x));
    for (int i = 2; i <= n; i++)
        if (x % i == 0)
            return false;
    return true;
}

int main()
{
    int n;
    cin >> n;
    bool found = false;
    for (int i = 2; i <= n; i++)
    {
        if (isprime(i) && isprime(i + 2))
        {
            found = true;
            cout << i << ' ' << i + 2 << '\n';
        }
    }
    if (!fount)
        cout << "empty";
    return 0;
}