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. SQL调用C# dll(第二中DLL,强名称密匙)

    参考:微软官网 https://msdn.microsoft.com/zh-cn/library/ms345106(es-es).aspx 1.新建项目 SQLDllTestUsingNew Clas ...

  2. 吴裕雄 python深度学习与实践(4)

    import numpy,math def softmax(inMatrix): m,n = numpy.shape(inMatrix) outMatrix = numpy.mat(numpy.zer ...

  3. TensorFlow 语法

    dataset = tf.data.TextLineDataset(file_path) 生成一个dataset,dataset中的每一个元素就对应了文件中的一行

  4. mySQL遇到的问题

    学习mySQL遇到以下错误. 仔细检查才发现,是字段不一样. 所以插入数据,应该一一对应.

  5. 解题(PockerCompare-扑克牌比较大小)

    题目描述 扑克牌游戏大家应该都比较熟悉了,一副牌由54张组成,含3~A.2各4张,小王1张,大王1张.牌面从小到大用如下字符和字符串表示(其中,小写joker表示小王,大写JOKER表示大王):3 4 ...

  6. django1.10使用本地静态文件

    django1.10使用本地静态文件方法 本文介绍的静态文件使用,是指启动web站点后,访问静态资源的用法,实际静态资源地址就是一个个的url 如果没有启动web站点,只是本地调试html页面,那直接 ...

  7. Pandas操作数据库及保存csv

    数据的保存 import pandas as pd import numpy as np from pandas import Series col_db = [['one',1,2,3,4,np.n ...

  8. tab template

    <div class="box"> <div class="box-body"> <div class="nav-tab ...

  9. sqoop2问题解决

    sqoop:000> show version --serverException has occurred during processing command Exception: org.a ...

  10. pta7-18奥运排行榜(模拟)

    题目链接:https://pintia.cn/problem-sets/1101307589335527424/problems/1101314114867245056 题意:给n个国家,以及每个国家 ...