题目

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 (< 105) 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

题目分析

可翻转质数:十进制中的质数,转换为指定进制并反转后的数字仍然是质数

已知十进制数,和指定进制,判断是否为可翻转质数

解题思路

  1. 判断输入的十进制数是否为质数
  2. 将输入的十进制数转换为指定进制数A
  3. 反转A得到B,判断B是否为质数

易错点

  1. 题目已知条件中数字为十进制,而不是指定进制下的数,虽然题目中未说明,但是可以从样例中23不可能是2进制判断出

知识点

  1. 题目没有给出样例数,输入非负数终止,该情况下的接收代码
while(scanf("%d",&n)!=EOF){
if(n<0)break;
}

Code

Code 01

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
bool isPrime(int n) {
if(n<=1)return false;
int sqr=(int)sqrt(1.0*n);
for(int i=2; i<=sqr; i++) {
if(n%i==0)return false;
}
return true;
}
int main(int argc, char * argv[]) {
int n,radix;
while(scanf("%d",&n)!=EOF) {
if(n<0)break;
scanf("%d",&radix);
if(isPrime(n)==false){
printf("No\n");
continue;
}
// 进制转换
// 十进制数转换为指定的radix进制
int index=0,d[111]={0};
do{
d[index++]=n%radix;
n/=radix;
} while(n!=0);
// 逆置的radix进制数转换为十进制数
for(int i=0;i<index;i++){
n=n*radix+d[i];
}
if(isPrime(n))printf("Yes\n");
else printf("No\n");
}
return 0;
}

PAT Advanced 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 (20)-素数

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

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

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

  4. PAT 甲级 1015 Reversible Primes(20)

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

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

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

  6. 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 ...

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

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

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

    转换进制&&逆序可以在一起进行,有一点技巧,不要用十进制数来表示低进制,容易溢出. #include <iostream> #include <vector> ...

  9. PAT 1015 Reversible Primes (判断素数)

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

随机推荐

  1. 干货分享|Critique Essay写作解析

    Critique essay要求学生对另一篇文章进行批判性分析,通常是一本书.期刊文章或论文.不管你的专业是什么,你可能会被要求在某个时候写一篇Critique essay.拿心理学专业举例,评论一篇 ...

  2. 吴裕雄--天生自然C++语言学习笔记:C++ 引用

    引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字.一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量 C++ 引用 vs 指针 引用很容易与指针混淆,它们之间有三个主要的 ...

  3. ACM-Alice and Bob

    题目描述:Alice and Bob One day, Alice asks Bob to play a game called “K-in-a-row”. There is a game board ...

  4. Kali链接Xshell和更新源

    一.Xshell首次链接kali系统中的ssh Xshell:帮助我们去连接各种服务平台,方便管理服务器,链路可以加密处理(ssh/vsftp) 1.开启kali中的ssh服务,service ssh ...

  5. 2016蓝桥杯省赛C/C++A组第六题 寒假作业

    题意:现在小学的数学题目也不是那么好玩的. 看看这个寒假作业: □ + □ = □ □ - □ = □ □ × □ = □ □ ÷ □ = □ 每个方块代表1~13中的某一个数字,但不能重复. 比如: ...

  6. 51nod 1429:巧克力

    1429 巧克力 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 现在有两个块巧克力一块大小是   的,另外一块大 ...

  7. MFC 屏蔽esc跟enter键

    BOOL CMenuOperate::PreTranslateMessage(MSG* pMsg) { if(pMsg->message == WM_KEYDOWN && pMs ...

  8. 服务器搭建---Linux安装Node.js

    先去官网下载:https://nodejs.org/en/download/ 把压缩包上传到服务器的/usr/local/soft(博主习惯)文件夹下  解压文件: cd /usr/local/sof ...

  9. spring 官方文档-片段学习——webflux-ann-controller

    spring 官方文档-片段学习总结 片段所在连接:https://docs.spring.io/spring/docs/5.0.4.RELEASE/spring-framework-referenc ...

  10. UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题

    很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...