1015 Reversible Primes(20 分)

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 (<10​5​​) and D (1<D≤10), 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

解题流程:

对于每行输入的两个数据N和D

将N转化为D进制的数(字符串),各位数字(字符)倒叙

倒叙后按进制D还原为10进制,得到数字rN

判断条件:如果rN和N都为素数,输出Yes,否则输出No

结束条件:N为负数

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int judge(int a) { //判断素数
if (a == 1)
return 0;
for (int i = 2; i <= sqrt(a); i++)
if (a%i == 0)
return 0;
return 1;
}
int trans(int N, int D) { //数字转化为D进制--逆转--还原10进制
string str;
for (; N != 0; N /= D)
str += to_string(N%D);
return stoi(str, 0, D);
}
int main() {
int N, D;
while (1) {
cin >> N >> D; //N 、D可分开判断N
if (N < 0) break;
if (judge(N) && judge(trans(N, D)))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}

PAT 甲级 1015 Reversible Primes(20)的更多相关文章

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

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

  2. PAT 甲级 1015 Reversible Primes

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

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

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

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

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

  5. 【PAT甲级】1015 Reversible Primes (20 分)

    题意: 每次输入两个正整数N,D直到N是负数停止输入(N<1e5,1<D<=10),如果N是一个素数并且将N转化为D进制后逆转再转化为十进制后依然是个素数的话输出Yes,否则输出No ...

  6. PAT 1015 Reversible Primes (20分) 谜一般的题目,不就是个进制转换+素数判断

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

  7. PAT (Advanced Level) Practice 1015 Reversible Primes (20 分)

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

  8. PAT (Advanced Level) 1015. Reversible Primes (20)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  9. PAT甲题题解-1015. Reversible Primes (20)-素数

    先判断n是否为素数然后把n转化成d进制下再反转,转化为十进制的num判断num是否为素数 注意n为0和1时,不是素数!!!注意反转后的num也有可能为1,不是素数!!! #include <io ...

随机推荐

  1. GIS工具-shp浏览器

    GIS工具-shp浏览器 软件特点: 1. 单个文件,windows平台 2. 绿色,不用安装 3.C语言系列开发,非vb,.net,Java等,无需虚拟机,无需运行时,无需第三方工具 获取方法: 十 ...

  2. 解决ngui挡住粒子的问题

    using UnityEngine; using System.Collections; [ExecuteInEditMode] public class ControlParticle : Mono ...

  3. Java学习04 (第一遍)

    封装.抽象.继承和多态.封装:在面向对象语言中,封装特性是由类来体现的,我们将现实生活中的一类实体定义成类,其中包括属性和行为(在Java中就是方法),就好像人类,可以具有name,sex,age等属 ...

  4. IT蓝豹强烈推荐:符合1-2年工作经验,开发中的难点及相关优化:

    IT蓝豹强烈推荐:符合1-2年工作经验,开发中的难点及相关优化: IT蓝豹 ------------------> sqlite数据库版本升级 1.sqlite升级步骤: 1.自己写一个类继承自 ...

  5. 利用原生态的(System.Web.Extensions)JavaScriptSerializer将mvc 前台提交到controller序列化复杂对象

    主要代码如下: public JsonResult Test() { string s = Request.Form.ToString(); JavaScriptSerializer jss = ne ...

  6. js判断是否为undefined

    typeof(isadmin)=="undefined"需要使用typeof才能判断

  7. php原样输出被编译过的html(适用于一些保存编辑器的内容)

    有时候有些编辑器写进数据库时尖括号或双引号被编译过,这时候输入来就是处理一下,不然得不到原样的 输入用  html_entity_decode($content)就可以了,有些直接存完整html的,输 ...

  8. Rendering with Replaced Shaders

    [Rendering with Replaced Shaders] 1.RenderType tag RenderType tag categorizes shaders into several p ...

  9. 游戏编程模式KeyNote

    [游戏编程模式KeyNote] 1.命令模式. 重做在游戏中并不常见,但重放常见.一种简单的重放实现是记录游戏每帧的状态,这样它可以回放,但那会消耗太多的内存.相反,很多游戏记录每个实体每帧运行的命令 ...

  10. ServiceWorker.state

    [ServiceWorker.state] ServiceWorker.state The state read-only property of the ServiceWorker interfac ...