PAT:1007

#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
//判断是否是素数
bool judge(int x)
{
    for (int j = 2; j <=sqrt(x); j++)
    {
        if (x % j == 0)
        {
            return false;
        }
    }
    return true;
}
int main()
{
    int n = 0;
    cin >> n;
    //防止输入越界
    while (n >= pow(10, 5))
    {
        cin >> n;
    }
    int count = 0;
    int temp = 0,i=2;
    while (i <=n)
    {
        temp = i;
        i++;
        if ((i + 1) > n)
        {
            break;
        }
        //因为之前i已经自加过,所以这里的i+1代表来两位之后的数字,素数对之间必定相差2
        if (judge(temp) && judge(i+1))
        {
            count++;
        }
    }
    cout << count << endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zongji/p/12241016.html