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
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int numReverse(int N, int radix){
int num[], temp, index = , ans = ;
do{
temp = N % radix;
num[index++] = temp;
N = N / radix;
}while(N != );
for(int i = index - , P = ; i >= ; i--){
ans += num[i] * P;
P = P * radix;
}
return ans;
}
int isPrime(int N){
int sqr = (int)sqrt(N * 1.0);
if(N == )
return ;
for(int i = ; i <= sqr; i++){
if(N % i == )
return ;
}
return ;
}
int main(){
int N, D, N2;
while(){
scanf("%d", &N);
if(N < )
break;
scanf("%d", &D);
int temp = numReverse(N, D);
if(isPrime(N) && isPrime(temp))
printf("Yes\n");
else printf("No\n");
}
cin >> N;
return ;
}

总结:

1、判断素数:

int isPrime(int N){
int sqr = (int)sqrt(N * 1.0); //N应该转换为小数
if(N == ) //当N = 1时应返回false,容易忽略,1不是素数
return ;
for(int i = ; i <= sqr; i++){ //i <= sqr; i从2开始查找
if(N % i == )
return ;
}
return ;
}

A1015. Reversible Primes的更多相关文章

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

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

  2. PAT甲级——A1015 Reversible Primes

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

  3. PAT_A1015#Reversible Primes

    Source: PAT A1015 Reversible Primes (20 分) Description: A reversible prime in any number system is a ...

  4. PAT 1015 Reversible Primes

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

  5. PAT 甲级 1015 Reversible Primes(20)

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

  6. PAT 1015 Reversible Primes[求d进制下的逆][简单]

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

  7. PTA (Advanced Level) 1015 Reversible Primes

    Reversible Primes A reversible prime in any number system is a prime whose "reverse" in th ...

  8. pat1015. Reversible Primes (20)

    1015. Reversible Primes (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A r ...

  9. pat 1015 Reversible Primes(20 分)

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

随机推荐

  1. flask-socketio笔记

    Flask-SocketIO使Flask应用程序可以访问客户端和服务器之间的低延迟双向通信. 客户端应用程序可以使用Javascript,C ++,Java和Swift中的任何SocketIO官方客户 ...

  2. C#_Math函数总结

    Math.abs() 计算绝对值. Math.acos() 计算反余弦值. Math.asin() 计算反正弦值. Math.atan() 计算反正切值. Math.atan2() 计算从x 坐标轴到 ...

  3. python基础学习笔记(一)

    安装与运行交互式解释器 在绝大多数linux和 UNIX系统安装中(包括Mac OS X),Python的解释器就已经存在了.我们可以在提示符下输入python命令进行验证(作者环境ubuntu) f ...

  4. 分页查询信息(使用jdbc连接mysql数据库实现分页查询任务)

             分页查询信息       使用jdbc连接mysql数据库实现分页查询任务 通过mysql数据库提供的分页机制,实现商品信息的分页查询功能,将查询到的信息显示到jsp页面上. 本项目 ...

  5. spring引入HikariCP连接池

    1.导入jar包 2.applicationContext.xml中配置 <bean id="dataSource" class="com.zaxxer.hikar ...

  6. github作业

    链接:   https://github.com/liuyu13/liuyu13-1 总结:git可以学习的东西还有很多.git协议,分布式协作,git项目管理,git技巧,github的使用和实践, ...

  7. 广商博客冲刺第三天new

    第二天沖刺傳送門 第三四天沖刺傳送門 这一天我们主要是弄网页前台设计跟框架设计,这方面主要是由张奇聪负责.我们在amaze ui,smart ui,angularjs+bootstrap中挑选,最终选 ...

  8. (转)SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM之间

    原因: 出现这种问题多半是因为你插入或者更新数据库时,datetime字段值为空默认插入0001年01月01日造成datetime类型溢出. 传给数据库表的时间类型值是null值.这里的null指的是 ...

  9. JavaScript 编程易错点整理

    Case 1: 通过getElementById("id")获得是一个DOM元素节点对象: 通过getElementsByTagName("tagName")获 ...

  10. Analyze a docker instance start failure

      错误信息:Cannot start container xxxxxxxxxxx | Error getting container xxxxxxxxxxxxxxx  from driver dev ...