当前位置:网站首页>洛谷回文质数 Prime Palindromes
洛谷回文质数 Prime Palindromes
2022-07-22 18:10:00 【潇湘夜雨寒】
洛谷回文质数
题目描述
因为 151 既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数。
写一个程序来找出范围 [a,b] (5 \le a < b \le 100,000,000)a,b( 一亿)间的所有回文质数。
输入格式
第 1 行: 二个整数 a 和 b .
输出格式
输出一个回文质数的列表,一行一个。
难点在于怎样不时间超限。
回文数的判断倒还行,一开始的时候曾经尝试着用string字符串的办法来写,不过后来速度有点慢,就放弃了。
素数的判断是此题关键:
1.若一个数的长度为偶数且是回文数,那么他一定不会是质数(11除外)
2. for (int i = 2; i <= sqrt(x); i++)此循坏可以判断出素数,但并不是最快的。
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
const int MAXN = 100000001;
int c[MAXN] = {
0 };
bool check1(int x)
{
if ((1000 <= x && x <= 9999) || (100000 <= x && x <= 999999)||(10000000 <= x && x <= 99999999))
return 0;
else
return 1;
}
bool flag(int x)
{
if (x == 2)
return 1;
for (int i = 2; i <= sqrt(x); i++)
if (x % i == 0)
return 0;
return 1;
}
bool flag1(int a)
{
int b = a;
int c = 0;
while (a != 0)
{
c = c * 10 + a % 10;
a = a / 10;
}
if (c == b)
return true;
else
return false;
}
int main()
{
int a, b;
cin >> a >> b;
if (b > 10000000)
b = 10000000;
for (int i = 0; i <=b; i++)
{
c[i] = 1;
}
c[0] = 0;
c[1] = 0;
for (int i = 2; i <=b; i++)
{
if (!c[i])
continue;
for (int j = i * 2; j <=b; j = j + i)
c[j] = 0;
}
for (int i = a; i <= b; i++)
{
if (flag1(i))
{
if(check1(i))
if(flag(i))
cout << i << endl;
}
}
return 0;
}
边栏推荐
- Amber tutorial 3.2: GPU viewing and running MD with pmemd engine
- 2021-06-03pip报错 ValueError: Unable to find resource t64.exe in package pip._vendor.distlib
- 存储过程
- Easy to understand, master interface automation-01
- 第二天总结及测试用例作业
- About the problem of re creation after deleting the module in idea:
- Ie settings - solve the problem of uploading upgrade packages with drive letter paths
- BeanShell 内置变量 ctx
- amber教程4.6:对体系氢键分析
- beanshell处理json的技巧
猜你喜欢

Software life cycle model ----- V model

Solutions to xftp upload errors

Day02 test case

Software bug

面向腾讯(实战)-测试开发-实习生(面经)详解

禅道的安装和使用&缺陷报告&缺陷作业

echo音箱配对及操作方法

Learn amber tutorial A17: umbrella sampling and drawing the potential energy surface of alanine tripeptide

Divide according to whether to check the original code: white box test and black box test

Test case: phone number
随机推荐
读《卓有成效的管理者-德鲁克》
Conditional statement of shell
关于使用VMD的对齐功能
机器视觉学习总结
postman提取response header中的token参数值,设置为环境变量,附代码
unittest框架学习(一)
About count=count++
Basic process of sales service
BeanShell内置变量vars的使用技巧
Day04 -- Installation of Zen path
KMP
第二天总结及测试用例作业
Software bug
[Huang ah code] getting started with MySQL - 1. SQL execution process
使用pip使用报错:pip is configured with locations that require TLS/SSL
面向腾讯(实战)-测试开发-实习生(面经)详解
À propos du montage de fond, de la gestion des processus
使用禅道的流程
Shallow shaking automatic test
Strategies of test case design and writing