Description
这一日,快码佳编四兄弟姐妹碰到了达数学家刘徽。是中国数学史上一个非常伟大的数学家,在世界数学史上,也占有杰出的地位.他的杰作《九章算术注》和《海岛算经》,是我国最宝贵的数学遗产。他们很快讨论起素数来了。在素数的大家庭中,大小相差为2的两个素数称之为一对“孪生素数”,如3和5、17和19等。请你编程统计出不大于自然数n的素数中,孪生素数的对数。
Input
多组测试数据,每组输入一个整数n,1 <=n <= 10000
Output
若干行,每行2个整数,之间用一个空格隔开,从小到大输出每一对孪生素数
Sample Input
100
Sample Output
3 5 5 7 11 13 17 19 29 31 41 43 59 61 71 73
思路:模拟O(n*sqrt(n))能过
#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5;
typedef long long LL;
LL a[maxn];//存素数
int main(void)
{
LL n;
while(cin>>n)
{
if(n==0) continue;
int cnt=1;
for(LL i=1;i<=n+10;i++) a[i]=0;
for(LL j=2;j<=n;j++)
{
int flag=1;
for(LL i=2;i<=j/i&&flag;i++)
{
if(j%i==0)
{
flag=0;
break;
}
}
if(flag==1)
{
a[cnt++]=j;
// cout<<"a["<<cnt<<"]="<<a[cnt]<<endl;
}
}
// for(LL i=1;i<=cnt;i++)
// cout<<a[i]<<endl;
for(LL i=1;i<=cnt;i++)
{
if(a[i+1]-a[i]==2)
{
cout<<a[i]<<" "<<a[i+1]<<endl;
}
}
}
return 0;
}