Problem 1: 回文素数

Problem E: 回文素数

Description
定义一个类SpecialPrime,只有一个静态成员函数

bool judge(int value)

用于判断value是否是一个回文素数。所谓回文素数是指一个数既是回文数又是素数。
Input
输入两个数m和n,0<m<n。

Output
区间[m,n]内的所有回文素数。

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

蛇皮大大留

这是第一题,这一年做满100道题应该有吧 冲冲冲

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读