A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.

Now given any two positive integers N (<) and D (1), you are supposed to tell if N is a reversible prime with radix D.

Input Specification:

The input file consists of several test cases. Each case occupies a line which contains two integers N and D. The input is finished by a negative N.

Output Specification:

For each test case, print in one line Yes if N is a reversible prime with radix D, or No if not.

Sample Input:

73 10
23 2
23 10
-2

Sample Output:

Yes
Yes
No

其实就是将输入的数字转化为该进制的反序列,然后再转化为10进制
然后判断是否是素数即可

 #include <iostream>
#include <math.h>
#include <string> using namespace std; bool isPrim(int a)
{
if (a < )
return false;
int b = (int)sqrt(1.0 * a);
for (int i = ; i <= b; ++i)
{
if (a%i == )
return false;
}
return true;
} int getReverNum(int a, int b)
{
string str = "";//得到反转的序列
while (a > )
{
str += a % b + '';
a /= b;
}
int n = ;
for (int i = str.length() - , j = ; i >= ; --i, ++j)
n += (str[i] - '')*pow(b, j);
return n;
} int main()
{
int a, b;
while (cin >> a)
{
if (a < )break;
cin >> b;
if (isPrim(a) && isPrim(getReverNum(a,b)))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return ;
}

PAT甲级——A1015 Reversible Primes的更多相关文章

  1. PAT 甲级 1015 Reversible Primes(20)

    1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...

  2. PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

    1015 Reversible Primes (20 分)   A reversible prime in any number system is a prime whose "rever ...

  3. PAT 甲级 1015 Reversible Primes

    https://pintia.cn/problem-sets/994805342720868352/problems/994805495863296000 A reversible prime in ...

  4. PAT A1015 Reversible Primes (20 分)——进制转换,质数

    A reversible prime in any number system is a prime whose "reverse" in that number system i ...

  5. A1015. Reversible Primes

    A reversible prime in any number system is a prime whose "reverse" in that number system i ...

  6. PAT Advanced 1015 Reversible Primes (20) [素数]

    题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...

  7. PAT_A1015#Reversible Primes

    Source: PAT A1015 Reversible Primes (20 分) Description: A reversible prime in any number system is a ...

  8. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  9. PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...

随机推荐

  1. 模板方法模式&策略模式区别联系

    一.模板方法 模板方法模式:定义 一系列算法, 子类延伸实现.着重点在于:子类去处理不同的方法实现. 看下面例子. 假如一个支付 都包含三个部分: 生成订单 ---->调用API发起支付---- ...

  2. P1919 【模板】A*B Problem升级版 /// FFT模板

    题目大意: 给定l,输入两个位数为l的数A B 输出两者的乘积 FFT讲解 这个讲解蛮好的 就是讲解里面贴的模板是错误的 struct cpx { double x,y; cpx(double _x= ...

  3. php 例子

    图片上传 uploadify(flash版是免费的) 12个最好的 HTML5 jQuery 文件上传脚本 20款最好的jQuery文件上传插件

  4. Foundation框架系列-NSArray

    NSArray常用API 数组字符串指定字符拼接 // 将数组中的元素以separator拼接返回字符串 比如@[@"a=1", @"b=2"] 以separa ...

  5. axel 参数 文件下载地址

    axel 参数 文件下载地址 可选参数: -n 指定线程数 -o 指定另存为目录 -s 指定每秒的最大比特数 -q 静默模式 实例 axel -n 10 -o /tmp/ http://testdow ...

  6. Android开发 ShapeDrawable详解

    前言 ShapeDrawable一开始我以为它是对应xml文件属性里的shape的画图,后来发现我错了... 其实ShapeDrawable更像是一共自由度更大跟偏向与实现draw()方法的一共图像绘 ...

  7. 180608发现的一个有趣的Douyin-Bot项目

    今日发现的github 项目 Douyin-Bot 抖音机器人发现漂亮小姐姐 :sweat_smile: 在开发者模式下,找到指针位置,开启之后,当点击屏幕时,可以获取点击屏幕处的xy坐标了 我使用的 ...

  8. CentOS7配置Docker镜像加速器

    1. 将默认的配置文件复制出来 cp -n /lib/systemd/system/docker.service /etc/systemd/system/docker.service 2. 将加速器地 ...

  9. python中遇到的问题:IndentationError: unexpected indent

    在Python中写下列代码的时候,出现错误:IndentationError: unexpected indent 分析:IndentationError是缩进的错误,查看源代码发现names开始的这 ...

  10. 简单搭建 @vue-cli3.0 及常用sass使用

    1,在安装了Node.js后使用其中自带的包管理工具npm.或者使用淘宝镜像cnpm(这里不做说明) 1-1,下载vue3.0脚手架(如果之前装vue-cli3x之前的版本,先卸载 npm unins ...