洛谷 P1217 回文质数

链接

https://www.luogu.org/problem/P1217

题目

题目描述

因为 151 既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数。

写一个程序来找出范围 [a,b] (5 <a < b < 100,000,000)( 一亿)间的所有回文质数。

输入格式

第 1 行: 二个整数 a 和 b .

输出格式

输出一个回文质数的列表,一行一个。

输入输出样例

输入 #1

5 500

输出 #1

5
7
11
101
131
151
181
191
313
353
373
383

说明/提示

Hint 1: Generate the palindromes and see if they are prime.

提示 1: 找出所有的回文数再判断它们是不是质数(素数).

Hint 2: Generate palindromes by combining digits properly. You might need more than one of the loops like below.

提示 2: 要产生正确的回文数,你可能需要几个像下面这样的循环。

题目翻译来自NOCOW。

USACO Training Section 1.5

产生长度为5的回文数:

for (d1 = 1; d1 <= 9; d1+=2) {    // 只有奇数才会是素数
for (d2 = 0; d2 <= 9; d2++) {
for (d3 = 0; d3 <= 9; d3++) {
palindrome = 10000*d1 + 1000*d2 +100*d3 + 10*d2 + d1;//(处理回文数...)
}
}
}

思路

说明不用看,直接编码就行了。我在LeetCode做过一样的题,不过这个对于性能要求更高一点。可以分成两个要求,质数,回文。

所有偶数位的回文数,都可以被11整除,证明么得,所以一下就能筛掉很多数,偶数不为质数,又可以筛掉一部分,之后就直接判断即可,本来我用的是字符处理,之后发现这样效率有点低,换成了数组比较,判断回文的开头和回文无关,只是我顺手把判断条件放那了,效率应该比放在质数那边高一点。

代码

#include<iostream>
#include<cmath>
#include<cstring> using namespace std; //质数
bool isPrime(int i)
{
for (int j = 2; j <= sqrt(i); j++)
if (i % j == 0)
return false;
return true;
} //回文
bool isHui(int s)
{
if((s>=1000 && s<10000)|| (s>=100000 && s<1000000) || (s>=10000000 && s<100000000))
return false; int a[10],i=0;
while(s>0)
{
a[i]=s%10;
s=s/10;
i++;
}
for(int j=0;j<i/2+1;j++)
{
if(a[j]!=a[i-j-1])
return false;
}
return true;
} int main()
{
int a,b;
cin>>a>>b;
for(int i=a;i<=b;i++)
{
if(i%2==0)
continue;
if(isHui(i) &&isPrime(i))
cout<<i<<endl;
}
return 0;
}

洛谷 P1217 回文质数的更多相关文章

  1. 洛谷P1217回文质数-Prime Palindrome回溯

    P1217 [USACO1.5]回文质数 Prime Palindromes 题意:给定一个区间,输出其中的回文质数: 学习了洛谷大佬的回溯写法,感觉自己写回溯的能力不是很强: #include &l ...

  2. 洛谷P1217 回文质数

    题目描述 因为 151 既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围 [a,b] (5 \le a < b \le 100,000 ...

  3. 洛谷 - P1217 - 回文质数 - 枚举

    https://www.luogu.org/problemnew/show/P1217 考虑暴力生成所有的回文数然后再判断是不是质数.注意个位的选择实际上只有4种.所以是 $4*10^3*10^3=4 ...

  4. 洛谷 P1015 回文数 Label:续命模拟QAQ

    题目描述 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数. 例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数. 又如:对于10进制数 ...

  5. 洛谷 P1015 回文数

    #include<iostream> #include<cstdio> #include<cmath> #include<string> #includ ...

  6. 洛谷 P2010 回文日期

    Noip2016普及组T2 题目描述 在日常生活中,通过年.月.日这三个要素可以表示出一个唯一确定的日期. 牛牛习惯用8位数字表示一个日期,其中,前4位代表年份,接下来2位代表月 份,最后2位代表日期 ...

  7. 洛谷 P2010 回文日期 题解

    P2010 回文日期 题目描述 在日常生活中,通过年.月.日这三个要素可以表示出一个唯一确定的日期. 牛牛习惯用88位数字表示一个日期,其中,前44位代表年份,接下来22位代表月 份,最后22位代表日 ...

  8. 洛谷 - P3649 - 回文串 - 回文自动机

    https://www.luogu.org/problem/P3649 #include <bits/stdc++.h> using namespace std; typedef long ...

  9. 洛谷P1435 回文子串

    题目背景 IOI2000第一题 题目描述 回文词是一种对称的字符串.任意给定一个字符串,通过插入若干字符,都可以变成回文词.此题的任务是,求出将给定字符串变成回文词所需要插入的最少字符数. 比如 “A ...

随机推荐

  1. OpenJudge 计算概论1007:点评赛车

    总时间限制: 1000ms 内存限制: 65536kB描述4名专家对4款赛车进行评论1)A说:2号赛车是最好的:2)B说:4号赛车是最好的:3)C说:3号赛车不是最好的:4)D说: B说错了.事实上只 ...

  2. Spark ML 中 VectorIndexer, StringIndexer等用法(转载)

    VectorIndexer 主要作用:提高决策树或随机森林等ML方法的分类效果.VectorIndexer是对数据集特征向量中的类别(离散值)特征(index categorical features ...

  3. wpf程序,只允许运行一个程序实例问题

    https://bbs.csdn.net/topics/390486402 https://codereview.stackexchange.com/questions/20871/single-in ...

  4. LeetCode_326. Power of Three

    326. Power of Three Easy Given an integer, write a function to determine if it is a power of three. ...

  5. 【Leetcode_easy】893. Groups of Special-Equivalent Strings

    problem 893. Groups of Special-Equivalent Strings 题意: 感觉参考代码也是有点问题的... 参考 1. Leetcode_easy_893. Grou ...

  6. 【Leetcode_easy】905. Sort Array By Parity

    problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...

  7. vue-cli3项目中全局引入less sass文件 以及使用本地图片在不同地方规则

    第一种直接在main.js中引入,需要声明loader demo: import '!style-loader!css-loader!less-loader!./assets/css/common.l ...

  8. Python3 IO编程之文件读写

    读写文件是最常见的IO操作.python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一个,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序终结操作磁盘, ...

  9. C# .NET 判断输入的字符串是否只包含数字和英文字母

    FROM :   https://www.cnblogs.com/ilookbo/p/4828722.html /// <summary> /// 判断输入的字符串是否只包含数字和英文字母 ...

  10. axios ajax框架 请求配置

    请求参数 { // `url` is the server URL that will be used for the request url: '/user', // `method` is the ...