UESTC 187 Prime Palindromes

1. 背景:原题链接:http://acm.uestc.edu.cn/#/problem/show/187

The number  151 151 is a prime palindrome because it is both a prime number and a palindrome (it is the same number when read forward as backward). Write a program that finds all prime palindromes in the range of two supplied numbers  a a and  b b ( 5a<b100,000,000 5≤a<b≤100,000,000); both  a a and  b b are considered to be within the range .

Input

Line  1 1: Two integers,  a a and  b b

Output

The list of palindromic primes in numerical order, one per line.

2. 分析:求闭区间[a, b]中的回文素数,此题考查"T"。

在碰到求素数或者一些有关时间限制时候,尽量剪枝,素数问题中应当“打表法”,且多做限界。

此处,“唉筛求素数”

void is_prime() {
    FOR(i, 2, MAXNum) {
        if(isPrime[i] == 0) {
            for(int j = i+i; j < MAXNum; j += i) {
                isPrime[j] = 1;
            }
        }
    }
}

3. 注意:原题中范围是到100000000, 但是1 0000000以上只有100000007为素数,但不是素数,因此可以大大剪枝。

回文就是将数字放在数组中,从0-n/2进行数组判等。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
//#include <map>
//#include <cmath>
//#include <queue>
//#include <stack>
//#define PB push_back
//#define PF pop_front
//#define sc1(x) scanf("%s",&(x))
#define sc(x) scanf("%d",&(x))
#define sc2(x,y) scanf("%d%d", &(x), &(y))
//#define sc3(x,y, z) scanf("%d%d%d", &(x), &(y), &(z))
#define pf(x) printf("%d\n", x);
//#define p(x) printf("%d ", x)
//#define P printf("\n")
//#define pf2(x, y) printf("%d %d\n", x, y)
#define FOR(i,b,e) for(int i=b; i<=e; i++)
#define FOR1(i,b,e) for(int i=b; i>=e; i--)
//#define FOR1(i,b,e) for(int i=b; i>e; i--)
#define CL(x,y) memset(x,y,sizeof(x))
using namespace std;

//const int MAXN = 100000000;
const int MAXNum = 10000000; // < 100000000, 只有100000007为素数
int a, b, arr[8], len;
int palindrome(int num);
int is_palindrome();
void is_prime();
bool isPrime[MAXNum];

int main() {
    CL(isPrime, 0);
    is_prime();

    sc2(a, b);
    FOR(i, a, b) {
        // 剪枝
        if(i>9989899)
            break;
        if(!isPrime[i] && palindrome(i)) {
            pf(i);
        }
    }
    return 0;
}
int palindrome(int num) {
    if(num < 10)
        return 1;
    else {
        int x = num;
        len = 0;
        FOR(i, 0, 7) {
            arr[i] = x % 10;
            x = x / 10;
            len++;
//            cout << num << endl;
            if(x == 0)
                break;
        }
    }
    return is_palindrome();
}
int is_palindrome() {

    FOR(i, 0, len / 2) {
        if(arr[i] != arr[len-1-i]) {
            return 0;
        }
    }
    return 1;
}
void is_prime() {
    FOR(i, 2, MAXNum) {
        if(isPrime[i] == 0) {
            for(int j = i+i; j < MAXNum; j += i) {
                isPrime[j] = 1;
            }
        }
    }
}




1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值