首页 > 其他 > 详细

PAT:1007

时间:2020-01-29 19:25:50      阅读:59      评论:0      收藏:0      [点我收藏+]

#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
//判断是否是素数
bool judge(int x)
{
    for (int j = 2; j <=sqrt(x); j++)
    {
        if (x % j == 0)
        {
            return false;
        }
    }
    return true;
}
int main()
{
    int n = 0;
    cin >> n;
    //防止输入越界
    while (n >= pow(10, 5))
    {
        cin >> n;
    }
    int count = 0;
    int temp = 0,i=2;
    while (i <=n)
    {
        temp = i;
        i++;
        if ((i + 1) > n)
        {
            break;
        }
        //因为之前i已经自加过,所以这里的i+1代表来两位之后的数字,素数对之间必定相差2
        if (judge(temp) && judge(i+1))
        {
            count++;
        }
    }
    cout << count << endl;
    return 0;
}

PAT:1007

原文:https://www.cnblogs.com/zongji/p/12241016.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!