题目

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. 068-PHP定义并输出数组

    <?php $arr=array(98,'hello',67,'A',85,NULL); //定义一个数组 echo "输出第一个元素:{$arr[0]}"; //输出数组的 ...

  2. css把图片方框变为圆角

    border-radius:10px; 多少就设多少像素,个人需求.

  3. python 嵌套爬取网页信息

    当需要的信息要经过两个链接才能打开的时候,就需要用到嵌套爬取. 比如要爬取起点中文网排行榜的小说简介,找到榜单网址:https://www.qidian.com/all?orderId=&st ...

  4. js模式-观察者模式

    // 主题,接收状态变化,触发每个观察者 class Subject { constructor() { this.state = 0 this.observers = [] } getState() ...

  5. UVA - 10570 Meeting with Aliens(外星人聚会)(暴力枚举)

    题意:输入1~n的一个排列(3<=n<=500),每次可以交换两个整数.用最少的交换次数把排列变成1~n的一个环状序列. 分析:正序反序皆可.枚举每一个起点,求最少交换次数,取最小值. 求 ...

  6. jquery特效 - 网站水波纹

    1,index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  7. SpringBoot基于easyexcel导出和写入Excel

      easyexcel是阿里巴巴旗下开源项目,主要用于Excel文件的导入和导出处理,今天我们利用SpringBoot和easyexcel实战演示如何导出和写入Excel文件. 一.加入我们需要的ea ...

  8. java课程之团队开发冲刺阶段2.4

    总结昨天进度: 1.照例学习了课前提醒的功能,不可否认的是,在这个功能上,需要的技术和之前的上课静音有点相似,都是通过广播然后开启service服务,然后进行每分钟的监听,查看时间是否一致,在一致的情 ...

  9. 深入X64架构(翻译)

    | 本人只是原创翻译,而且翻译也不一定好,纯当锻炼.内容如果英文好的同学,建议直接去看英文原版,比较爽. NBAOL系列2代产品是 windows平台64位的应用程序,在技术测试过程中,遇到一些cra ...

  10. GPRS模块

    一.参考网址 1.AT指令(中文详解版)(二)