编写程序,输出前100个回文素数,每行打印10个,适当对齐,只能用c++,不能用c语言
#include<iostream>
#include<cmath>
using namespace std;
int ss(long n)
{
if(n<2)return 0;
else if(n==2) return 1;
for(int i=2;i<=sqrt(n);i++)
if(n%i==0)return 0;
return 1;
}
long hw(long n)
{ long t,i,j,k;
t=n;
k=0;
while(t>0)
{
k=t%10+k*10;
t/=10;
}
if(k==n) return 1;
else return 0;
}
int main()
{ long n=2,i=0;
while(i<100)
{
if(ss(n)>0&&hw(n)>0) {
cout<<n<<"\t";
i++;
if(i%10==0) cout<<endl;
}
n++;
}
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!