题目

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. 一百一十五、脱离SAP本体,通过ActiveX读取SAP表中数据

    一.Sap自带有客户端,但是非常之臃肿卡顿,可以利用ActiveX的方式,脱离Sap本体,来读取Sap表中的内容进行插入等操作,非常之方便.代码如下: 二.界面如下,输入好相关内容,点击登录,提示登录 ...

  2. HDU 4866 多校1 主席树+扫描线

    终于是解决了这个题目了 不过不知道下一次碰到主席树到底做不做的出来,这个东西稍微难一点就不一定能做得出 离散化+扫描线式的建树,所以对于某个坐标二分找到对应的那颗主席树,即搜索出结果即可(因为是扫描线 ...

  3. VIJOS-P1282 佳佳的魔法照片 排序

    Description 一共有n(n≤20000)个人(以1--n编号)向佳佳要照片,而佳佳只能把照片给其中的k个人.佳佳按照与他们的关系好坏的程度给每个人赋予了一个初始权值W[i].然后将初始权值从 ...

  4. JavaScript(第十一天 9.24)

    Html(决定页面显示的数据)+CSS(决定页面的布局)+JS(和用户或者服务器交互.决定页面的行为) JS前端的编程语言,脚本语言,简单易学,基础是HTML+CSS 编译工具使用:vscode  s ...

  5. 每天一点点之vue框架开发 - vue 动态替换路由(地址栏)参数

    import merge from 'webpack-merge': // 修改原有参数 this.$router.push({ query:merge(this.$route.query,{'max ...

  6. opencv 矩阵操作

    OpenCv矩阵操作 有很多函数有mask,代表掩码,如果某位mask是0,那么对应的src的那一位就不计算,mask要和矩阵/ROI/的大小相等 大多数函数支持ROI,如果图像ROI被设置,那么只处 ...

  7. B站 React教程笔记day1(4)调色板案例

    视频地址 main.js import React from "react" import { render } from "react-dom" import ...

  8. Day 10:浅谈正则表达式

    正则表达式 以检验扣扣号是否合法为例引入正则表达式 要求:校验QQ号,要求:必须是5~15位数字,0不能开头. 1.没有正则表达式 public class Demo1 { public static ...

  9. sqlcook sql经典实例 emp dept 创建语句

    创建表语句 create table dept( deptno int primary key, dname varchar(30), loc varchar(30) ); create table ...

  10. Java web项目所需技术*(大概)

    实施java 的web项目需要掌握的技术如下: 1.java语言 2.  面向对象分析设计 XML 语言 网页脚本语言 数据库 应用服务器 集成开发环境 .java语言:JSP.Servlet.JDB ...