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. ArcGIS自定义工具箱-判断字段值是否相等

    ArcGIS自定义工具箱-判断字段值是否相等 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:判断两个字段值是否相等 使用方法: 结果: 联系方式:谢老师,13 ...

  2. Swift类型转换 和 类型别名的定义(typealias)

    (一)类型转换 类型转化在 Swift 中是比较严格的,不同类型之间可以认为是不能相互转化的,只能重新产生一个对象和值,并拷贝一份. 1.0 整型数值之间的转换. // 不同类型是不能直接相加的,这时 ...

  3. yum源制作

    CentOS7 同步远程镜像 搭建本地yum服务器同步CentOS镜像站点的数据到本地服务器,使用nginx实现http服务向局域网内的其他机器提供yum服务,解决内网yum安装软件的问题. 一.前提 ...

  4. servlet中的执行顺序

  5. HTML 设置字体

    HTML,CSS,font-family:中文字体的英文名称 (宋体 微软雅黑)     宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsof ...

  6. redis滴

    Redis 可用于内存存储,也可以基于持久化存储 Key-Value的形式存储. Redis的数据结构 1.字符串(string) 2.字符串列表(lists) 3.字符串集合(sets) 4.有序字 ...

  7. kotlin函数api

    原 Kotlin学习(4)Lambda 2017年09月26日 21:00:03 gwt0425 阅读数:551   记住Lambda的本质,还是一个对象.和JS,Python等不同的是,Kotlin ...

  8. spring 手册

    https://www.tutorialspoint.com/spring/spring_architecture.htm

  9. VSC KeyNote

    [VSC KeyNote] 1.前后跳转. Alt + LeftArrow, Alt + RightArrow 2.缩进问题. vsc默认缩进为4,但js代码里缩进依旧是2. 因为vscode默认启用 ...

  10. spring boot 中统一异常处理

    基于 spring boot 对异常处理的不友好,现在通过其他的方式来统一处理异常 步骤一:自定义异常类 public class UserNotExistException extends Runt ...