1007 素数对猜想(20分)

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int n,count = 0,before = 2,w=0;
    cin>>n;
    for(int i=2;i<=n;i++)
    {
        w=0;
        for(int j=2;j<=sqrt(i);j++)
        {
            if(i%j==0)
            {
                w=1;//不是素数
                break;
            }
        }
        if(w==1)
            continue;
        if(i-before==2)
            count++;
        before=i;
    }
    cout<<count<<endl;
    return 0;
}

判断素数的时候一定要用sqrt,不然会超时。

原文地址:https://www.cnblogs.com/QRain/p/12218789.html