1015 Reversible Primes (20)(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

radix是进制的意思,判断一个数是否是素数,如果不是则输出no,如果是则在D进制下进行Reverse,反转之后如果还是素数,那么就输出yes.

//如何求d进制的逆,参考:https://blog.csdn.net/sunbaigui/article/details/8657051

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
bool isPrime(int n){
int m=sqrt(n);
if(n==||n==)return false;
for(int i=;i<=m;i++){
if(n%i==)return false;
}
return true;
}
int rever(int n,int d){
// string s="";求d进制,逆的方法
// int m;
// while(n!=0){
// m=n%d;
// n/=d;
// s+=(m+'0');
// }
// cout<<s<<" ";
// n=0;
// m=s.size();
// for(int i=0;i<m;i++){
// n=n*d+(s[i]-'0');//这样求也是对的,但是明显比较复杂。
// }
int sum=;
do{
sum=sum*d+n%d;//求逆!
n/=d;
}while(n!=);
return sum;
} int main()
{
int n,d;
while(scanf("%d%d",&n,&d)!=){
//判断n是否是素数
if(isPrime(n)){
n=rever(n,d);
if(isPrime(n))
printf("Yes\n");
else
printf("No\n");
}else
printf("No\n");
}
return ;
}

1.求逆一个do-while循环太厉害了,注释掉的部分是我写的,不太对。

2.要注意特殊条件的判断,n==0||n==1直接返回false;不是素数,要不然,样例只能通过两个,另两个通不过!special case.

PAT 1015 Reversible Primes[求d进制下的逆][简单]的更多相关文章

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

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

  2. PAT 1015 Reversible Primes

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

  3. pat 1015 Reversible Primes(20 分)

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

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

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

  5. PAT-1015 Reversible Primes (20 分) 进制转换+质数

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

  6. 数位DP 求K进制下0~N的每个数每位上出现的数的总和

    好久没写博客了,因为感觉时间比较紧,另一方面没有心思,做的题目比较浅也是另一方面. 热身赛第二场被血虐了好不好,于是决定看看数位DP吧. 进入正题: 如题是一道经(简)典(单)的数位dp. 第一步,对 ...

  7. PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

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

  8. PAT A1015 Reversible Primes (20 分)——进制转换,质数

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

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

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

随机推荐

  1. [原]Failed to load SELinux policy. System Freezing ----redhat7or CentOS7 bug

    重启rhel7或者centos7 启动界面按 e 在启动项后面加上enforcing=0 Ctrl+x  运行修改后的grub 进入系统 编辑保存/etc/selinux/config 重启

  2. 【CF744D】Hongcow Draws a Circle 二分+几何

    [CF744D]Hongcow Draws a Circle 题意:给你平面上n个红点和m个蓝点,求一个最大的圆,满足圆内不存在蓝点,且至少包含一个红点. $n,m\le 10^3$ 题解:我们先不考 ...

  3. Android 应用内切换语言

    extends :http://bbs.51cto.com/thread-1075165-1.html,http://www.cnblogs.com/loulijun/p/3164746.html 1 ...

  4. Spark2 AccumulatorV2累加器

    Accumulator.scala  (Since version 2.0.0) use AccumulatorV2 import org.apache.spark.util._ val accum= ...

  5. Deplearning.AI-吴恩达【中文课后作业】

    [吴恩达课后作业目录] 课程 周数 名称 类型 语言 地址 课程1 - 神经网络和深度学习 第1周 深度学习简介 测验 中英 传送门 无编程作业 编程作业 —— —— 第2周 神经网络基础 测验 中英 ...

  6. 那些你觉得堪称神兵利器的 Chrome 插件

    子曾曰:"工欲善其事,必先利其器.居是邦也."--语出<论语·卫灵公>:其后一百多年,荀子也在其<劝学>中倡言道:"吾尝终日而思矣,不如须臾之所学 ...

  7. CodeForces - 950C Zebras 模拟变脑洞的天秀代码

    题意:给你一个01串,问其是否能拆成若干形如0101010的子串,若能,输出所有子串的0,1 的位置. 题解:一开是暴力,然后瞎找规律, 最后找到一种神奇的线性构造法:扫一遍字符串,若为0就一直竖着往 ...

  8. UIGestureRecognizer和UITouch

    UIGestureRecognizer和UITouch是分别判断的,如果判定了是手势,那就不再触发UITouch事件,如果两者并存,则会先执行UITouch事件,之后如果确认是手势,不再执行UITou ...

  9. TensorFlow基础1:reduce_sum()函数和reduce_mean()函数

    https://blog.csdn.net/chengshuhao1991/article/details/78545723 在计算损失时,通常会用到reduce_sum()函数来进行求和,但是在使用 ...

  10. html中label及加上属性for之后的用法

    定义和用法 <label> 标签为 input 元素定义标签(label). label 元素不会向用户呈现任何特殊的样式.不过,它为鼠标用户改善了可用性,因为如果用户点击 label 元 ...