#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,不然会超时。