这个题开始我把10^5次方当成10000了。并且因为PAT对时间的要求不高,所以不用筛法求素数也可通过。。
//============================================================================
// Name : pat.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
short array[100000];
//1 质数
//-1 合数
//0 未知
void process()
{
array[0]=array[1]=-1;
for(int i=2;i<100000;i++)
{
if(array[i]!=0)
{
continue;
}
array[i]=1;
int temp=i*2;
while(temp<100000)
{
array[temp]=-1;
temp+=i;
}
}
}
int getNext(int input)
{
input++;
while(input<100000&&array[input]!=1)
{
input++;
}
return input;
}
int main() {
process();
int number;
cin>>number;
int count=0;
int pre=2;
int aft=getNext(pre);
for(;aft<=number&&pre<aft;pre=aft,aft=getNext(aft))
{
if(aft-pre==2)
{
count++;
}
}
cout<<count<<endl;
}
本文深入探讨了使用C++编程语言解决查找连续素数的问题,特别是通过预处理生成素数表来优化查找过程。文章详细介绍了算法实现步骤,并提供了具体的代码实例,帮助读者理解如何在不依赖于复杂数学算法的情况下高效解决问题。
1031

被折叠的 条评论
为什么被折叠?



