A1015. Reversible Primes
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
#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的更多相关文章
- PAT A1015 Reversible Primes (20 分)——进制转换,质数
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
- PAT甲级——A1015 Reversible Primes
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
- PAT_A1015#Reversible Primes
Source: PAT A1015 Reversible Primes (20 分) Description: A reversible prime in any number system is a ...
- PAT 1015 Reversible Primes
1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "rever ...
- PAT 甲级 1015 Reversible Primes(20)
1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...
- PAT 1015 Reversible Primes[求d进制下的逆][简单]
1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...
- PTA (Advanced Level) 1015 Reversible Primes
Reversible Primes A reversible prime in any number system is a prime whose "reverse" in th ...
- pat1015. Reversible Primes (20)
1015. Reversible Primes (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A r ...
- pat 1015 Reversible Primes(20 分)
1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...
随机推荐
- 收藏pdf 链接
python 入门: https://files.cnblogs.com/files/minsons/python%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E6%B7% ...
- WordPress更新提示无法创建目录的解决方案
上一篇我们说到无法连接FTP服务器,我们已经完美的解决了,然后...发现...还是无法更新,啥情况??? 提示为无法创建目录 原因是执行更新程序的是www用户, 解决方案如下: 需要把插件或主程序下载 ...
- Js把Json序列化为Java接受的对象。
服务器端 Java定义 data class role(var name: String = "", var remark: String = "") data ...
- linux上启动tomcat远程不能访问
linux上关闭防火墙同样访问不了,执行iptables -f即可. 你试一试这个“iptables -F”然后再访问,如果能够访问了,那么需要执行“firewall-cmd --add-port=8 ...
- linux-流程控制语言
if: for: 增强for循环 while: 统计这个目录下所有文件的大小 编写脚本 执行 help text:
- 关于QQ的NABCD模型
关于QQ的NABCD模型 N--Need 随着电脑的普及,人们在网络上进行交流的时间越来越多,由于现有的交流工具还不是那么的完善,还不能够完全满足人们在交流时的需求.因此为了满足人们更多的需求,我们设 ...
- Magazine Ad CodeForces - 803D (二分+贪心)
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad ...
- HDU 2032 杨辉三角
http://acm.hdu.edu.cn/showproblem.php?pid=2032 Problem Description 还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考 ...
- Redis交互编程语言及客户端
Redis Desktop Manager https://redisdesktop.com/download Redis Clients https://redis.io/clients/ ...
- BUG管理工具——Mantis安装配置
配置环境: CentOS6.5(所有操作在root用户下面操作) 1. 关闭防火墙, service iptables stop(防止防火墙捣乱,或者还得手动添加端口号的麻烦) 2. Disable ...