Problem 1: 回文素数

Problem E: 回文素数

Description

bool judge(int value)

Input

Output

Sample Input
2 1000
Sample Output
2
3
5
7
11
101
131
151
181
191
313
353
373
383
727
757
787
797
919
929

算法一

#include<bits/stdc++.h>

using namespace std;

int prime[1000010];

class SpecialPrime
{
private:

public:

static int flag;
static bool judge(int value)
{
int s[1000];
int cnt = 0;
if(flag==0)
{
make_prime();
flag = 1;
}
if(prime[value]==1)
{
return false;
}

while(value!=0)
{
s[cnt] = value%10;
value = value/10;
cnt++;
}
for(int i = 0; i<cnt; i++)
{
if(s[i]!= s[cnt-1-i])
{
return false;
}
}
return true;
}
static void make_prime()
{
memset(prime,0,sizeof(prime));
for(int i =2; i<sqrt(1000000); i++)
{
if(prime[i] == 0)
{
for(int j = i*i; j<1000000; j=j+i)
{
prime[j] = 1;
}
}
}
}
SpecialPrime()
{
};
~SpecialPrime()
{

};
};

int SpecialPrime::flag = 0;

int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int m, n, i;
cin>>m>>n;
for (i = m; i < n; i++)
{
if (SpecialPrime::judge(i))
cout<<i<<endl;
}
return 0;
}


算法二

#include <bits/stdc++.h>

using namespace std;

bool isPrimer(int a)
{
bool yes = true;
if (a<2)
yes = false;
for (int i=2; i*i<=a; i++)
{
if (a%i==0)
{
yes = false;
break;
}
}
return yes;
}

bool isPalindrome(int x)
{
// 负数的第一位带有负号'-'，所以一定不是回文数
// 长度超过一位的数字，第一位肯定不是 0，因此末尾是 0，则一定不是回文数
if (x < 0 || (x % 10 == 0 && x != 0))
return false;
// 余下的代码，如果看不懂原理，就带入几个具体的数字，手工模拟运行过程，就很容易明白了
int revertedNumber = 0;
while (x > revertedNumber)
{
revertedNumber = revertedNumber * 10 + x % 10;
x /= 10;
}
return x == revertedNumber || x == revertedNumber / 10;
}

class SpecialPrime
{
public:
static bool judge(int value)
{
if((isPrimer(value)==true)&&(isPalindrome(value)==true))
return true;
return false;
}
};

int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int m, n, i;
cin>>m>>n;
for (i = m; i < n; i++)
{
if (SpecialPrime::judge(i))
cout<<i<<endl;
}
return 0;
}


算法三

#include <bits/stdc++.h>
using namespace std;

int sh[115] = {1,2,3,5,7,11,101,131,151,181,191,313,353,373,383,727,757,787,797,919,929,10301,10501,10601,11311,
11411,12421,12721,12821,13331,13831,13931,14341,14741,15451,15551,16061,16361,16561,16661,17471
,17971,18181,18481,19391,19891,19991,30103,30203,30403,30703,30803,31013,31513,32323,32423,33533,34543,34843,35053,35153,35353,35753,36263,36563,37273,37573,38083,38183,38783,39293,70207,70507,
70607,71317,71917,72227,72727,73037,73237,73637,74047,74747,75557,76367,76667,77377,77477,77977,78487,
78787,78887,79397,79697,79997,90709,91019,93139,93239,93739,94049,94349,94649,94849,94949,95959,96269,96469
,96769,97379,97579,97879,98389,98689};

bool hws(int value){

int a[10001];
int i ;
for(i =1;value != 0;i++)
{
a[i] = value%10;
value /= 10;
}
i--;
for(int j=1;j<i;j++,i--){
if(a[j] != a[i]){
return false;
}
}
return true;
}
bool susu(int value){

for(int i=2 ;i<=sqrt(value);i++){

if(value %i == 0){
return false;
}
}
return  hws(value);
}
class SpecialPrime{

public:
static bool judge(int value){
for(int i =0;i<115;i++){
if(sh[i] == value){
return true;
}
}
return false;
}
};

int main()
{
int m, n, i;
cin>>m>>n;
for (i = m; i < n; i++)
{
if (SpecialPrime::judge(i))
cout<<i<<endl;
}
return 0;
}


2020.05.23

蛇皮大大留

10-14 297

04-14 35

05-23 46

06-04 636

03-15 110

03-29 15

02-22

11-12 7178

08-16 1万+

01-12 356

04-28 701

01-18 324

07-30 223

04-23 560

05-26 1527

12-01 322

03-04 14万+

03-08 5万+

03-05 6万+

05-03 16

03-08 7万+

03-01 14万+

02-28 4万+

01-29 64

02-27 8万+

03-10 13万+

04-14 60万+

03-13 15万+

程序员请照顾好自己，周末病魔差点一套带走我。

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客