/*
* @Author: 365JHWZGo
* @Description: 1007 素数对猜想 (20 分)
* @Date: 2021-09-02 10:36:18
* @FilePath: \VScodeC++practice\PAT\sushuAssume.cpp
* @LastEditTime: 2021-09-02 11:35:19
* @LastEditors: 365JHWZGo
*/
#include <iostream>
#include <math.h>
using namespace std;
bool judge(int n)
{
//最后一个测试点要使用sqrt(n)+1,否则运行超时
for (int i = 2; i < sqrt(n) + 1; i++)
{
if (n % i == 0)
return false;
}
return true;
}
int main()
{
//std::ios::sync_with_stdio(false);
int num, ans = 0;
cin >> num;
int before = 2, now = 0;
//i<=num,否则当等于num时不执行
for (int i = 3; i <= num; i += 2)
{
if (judge(i))
{
now = i;
if (now - before == 2)
ans++;
before = now;
}
}
cout << ans;
return 0;
}
1007 素数对猜想 (20 分)满分!
最新推荐文章于 2024-09-08 15:17:59 发布
本文介绍了PAT编程竞赛中的一道题目——1007素数对猜想,作者提供了C++代码实现,通过判断奇数是否为素数来寻找连续的素数对,并统计其数量。代码中特别注意了在最后一个测试点使用sqrt(n)+1以避免超时问题。
摘要由CSDN通过智能技术生成