zoukankan      html  css  js  c++  java
  • PAT Basic 1007

    1007 素数对猜想 (20 分)

    让我们定义dn​​为:dn​​=pn+1​​pn​​,其中pi​​是第i个素数。显然有d1​​=1,且对于n>1有dn​​是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。

    现给定任意正整数N(<105​​),请计算不超过N的满足猜想的素数对的个数。

    输入格式:

    输入在一行给出正整数N

    输出格式:

    在一行中输出不超过N的满足猜想的素数对的个数。

    输入样例:

    20
    

    输出样例:

    4


    注意是不超过,n也需要算进去
    #define _CRT_SECURE_NO_WARNINGS
    #include<iostream>
    #include <vector>
    #include<algorithm>
    #include<string>
    #include<math.h>
    #define max 100000
    #define debug 0
    using namespace std;


    int main() {
    #if debug
        freopen("in.txt", "r", stdin);
    #endif

        int n = 0,a[max],num=0,count=0;
        cin >> n;
        for (int i = 2; i <=n; i++)
        {
            int j = 2;
            for (; j <sqrt(i); j++)
            {
                if (i%j == 0)
                    break;
            }
            if (j > sqrt(i))
            {
                a[num++] = i;
            }
        }

        for (int i = 0; i < num-1; i++)
        {
            if (a[i + 1] - a[i] == 2)
            {
                count++;
            }
        }
        cout << count<<endl;

    #if debug
        freopen("CON", "r", stdin);
    #endif
        return 0;
    }
  • 相关阅读:
    原创 128的二进制有原码 反码和补码
    asp.net中进行获取当前时间以及当前时间的前后几个月
    dropdownlist的使用
    CutEditor在线编辑器的使用
    Repeater 嵌套用法
    使用cuteeditor进行视频上传
    电脑知识大全(快捷键大全)
    web.config
    项目总结(大唐)
    上传视频和图片的同时,生成缩略图
  • 原文地址:https://www.cnblogs.com/lxzbky/p/10497846.html
Copyright © 2011-2022 走看看