Prime Palidromes_回文素数_usaco1.5_枚举?

DESCRIPTION

The number 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 and b (5 <= a < b <= 100,000,000); both a and b are considered to be within the range .

PROGRAM NAME: pprime

INPUT FORMAT

Line 1: Two integers, a and b

OUTPUT FORMAT

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

HINTS (use them carefully!)

HINT1 HINT2

TRANSLATION

找到给粗区间[a..b]中的所有回文素数

ANALYSIS

突然想起还有usaco这么个题库 不要在意之前干嘛去了

咳咳,枚举a~b区间的数字,分别判断是不是回文数和素数

玄学优化:
1. 除5外,以5,0结尾的数字是合数,false
2. 除2外,以偶数结尾的数字是合数,false
3. 除11外,偶数数位的回文数字是合数,false
4. 最大不可能超过9989899 我也不知道为什么

CODE

/*
ID:wjp13241
PROG:pprime
LANG:C++
*/

#include <stdio.h>
#include <cmath>

using namespace std;

int prime(int x)
{
    for (int i=2;i<=(int)(sqrt(x));i++)
        if (!(x%i))
            return 0;
    return 1;
}

int palindrome(int x)
{
    int f[10]={0};
    while(x)
    {
        f[++f[0]]=x%10;
        x/=10;
    }

    int l=0;
    while (++l<=f[0]/2)
    {
        if (f[l]!=f[f[0]-l+1])
            return 0;
    }
    return 1;
}

int main()
{
    freopen("pprime.in","r",stdin);
    freopen("pprime.out","w",stdout);

    int a,b;
    scanf("%d%d",&a,&b);
    a=a==1?2:a;
    b=b>9989899?9989899:b;
    for (int i=a;i<=b;i++)
    {
        if (!palindrome(i))
            continue;

        if (!prime(i))
            continue;

        printf("%d\n",i);
    }

    fclose(stdin);
    fclose(stdout);
    return 0;
}

转载于:https://www.cnblogs.com/olahiuj/p/5781227.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值