【pat】b1007 素数对猜想

  1. 素数筛筛出规定范围内的所有素数存入数组
  2. 遍历数组,如果满足于后边的差2,计数器加加
    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 <cstdio>
    const int maxn = 10000001;
    int prime[maxn]={0};
    bool p[maxn] = {0};
    int pnum =0;
    void Find_prime(int n)
    {
    for(int i=2; i<=n; i++)
    {
    if(p[i] == false)
    {
    prime[pnum++] = i;
    for(int j=i+i; j<= n; j+=i)
    {
    p[j] = true;
    }
    }
    }
    }
    int main()
    {
    int N,count=0;//素数范围
    scanf("%d",&N);
    Find_prime(N);//找到N以内的所有素数存入数组prime
    for(int i=0; i<pnum; i++){//遍历素数表
    if(prime[i+1] - prime[i] == 2)
    count++;
    }
    printf("%d",count);
    return 0;
    }