PAT乙级1007.素数对猜想

查看源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<math.h>
using namespace std;
int a[100000]={0};
int prime(int c)
{int i;  
 if(c%2==0&&c!=2)
  return 0;
 for(i=3;i<=sqrt(c);i++) //然后用i(从2到k,即m的平方跟)去除m, 
 if(c%i==0) return 0; //如果能被整除, 则不是素数,break 
return c!= 1;  
  
}
int main()
{int n,count,j;
 cin>>n;
 count=0;
 j=0;
 int m; 
for(m=1;m<=n;m++) //m=m+2,因为偶数都不是素数,不用考虑,所以每次m+2. 
{
 if(prime(m)) 
         a[j++]=m;
         
}
  for(int q=0;q<j-1;q++)
         if(a[q+1]-a[q]==2)
             count++;
  cout<<count; 

}
posted @ 2018-01-29 18:56  kong孔  Views(73)  Comments(0)    收藏  举报