PAT-1015 Reversible Primes (20 分) 进制转换+质数
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,3和负数的情况
#include<bits/stdc++.h>
#define de(x) cout<<#x<<" "<<(x)<<endl
#define each(a,b,c) for(int a=b;a<=c;a++)
using namespace std;
const int maxn=50+5;
int buf[maxn];
bool isprime(int x)
{
if(x<=1)return false;
bool flag=true;
if(x==2)return true;
if(x==3)return true;
for(int i=2;i*i<=x;i++)
{
if(x%i==0)
{
flag=false;
break;
}
}
return flag;
}
int main()
{
//de(isprime(73));
int n,r;
while(true)
{
cin>>n;
if(n<0)break;
cin>>r;
if(n==1||n==0)
{
puts("No");
continue;
}
if(!isprime(n))///读题的问题,首先得判断他是不是一个质数才行,
{
puts("No");
continue;
}
int len=0;
while(n>0)
{
buf[len++]=n%r;
n/=r;
}
//de(len);
int sum=0;
len--;
for(int i=0;i<=len;i++)
{
sum+=buf[i]*pow(r,len-i);
}
//de(sum);
if(isprime(sum))puts("Yes");
else puts("No");
}
}
PAT-1015 Reversible Primes (20 分) 进制转换+质数的更多相关文章
- PAT 1015 Reversible Primes[求d进制下的逆][简单]
1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...
- PAT 1015 Reversible Primes (20分) 谜一般的题目,不就是个进制转换+素数判断
题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...
- PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))
1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "rever ...
- PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642
PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...
- 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 ...
- 【PAT甲级】1015 Reversible Primes (20 分)
题意: 每次输入两个正整数N,D直到N是负数停止输入(N<1e5,1<D<=10),如果N是一个素数并且将N转化为D进制后逆转再转化为十进制后依然是个素数的话输出Yes,否则输出No ...
- 1015 Reversible Primes (20 分)
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
- pat 1015 Reversible Primes(20 分)
1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...
- PAT Advanced 1015 Reversible Primes (20) [素数]
题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...
随机推荐
- final和finally的区别
final关键字可以用于修饰类,方法,变量.用该关键字修饰类,方法,变量都有不可变的特性. 1)final关键字用于基本数据类型前,就表明该变量就变成了一个常量,在被定义后的赋值不能被修改. 2)fi ...
- MediaPlayer: BufferQueue has been abandoned 解决方案
最近在做一个播放器,需要实现一个从小屏切换到全屏的效果,我用的是TextureView + MediaPlayer 的方式去实现,当需要切换成全屏时,我在 TextureView 的 onSurfac ...
- win10下安装Kafka
去kafka官网(http://kafka.apache.org/downloads.html)下最新包(目前是2.3.0),不分操作系统,直接点二进制压缩包链接跳过去下载即可 -> 解压到你指 ...
- 阶段5 3.微服务项目【学成在线】_day17 用户认证 Zuul_03-用户认证-认证服务查询数据库-查询用户接口-接口定义
1.2.4 查询用户接口 完成用户中心根据账号查询用户信息接口功能. 在ucenter这个服务里面定义查询用户信息的接口 这个接口在auth的服务的loadUserByUserName这个方法里面被调 ...
- WinSCP 上传文件至Cetos 7 后用户无权限
WinSCP是一个支持SSH的SCP文件传输软件. 可以用Windows环境向Linux环境传输文件,今天给新的Elasticsearch 服务器(cetos 7 )部署新的集群节点的时候,发现传输后 ...
- mysql创建数据库、表、写数据
mysql> create database ceshi; Query OK, 1 row affected (0.01 sec) 给数据库授权,否则程序时无法连接ceshi数据库的,每次创建一 ...
- Node.js使用MySQL连接池示例
下面是一个封装好的工具类: var fs = require('fs'); var mysql = require('mysql'); var pool = mysql.createPool({ ho ...
- Python3之类和实例访问限制
在Class内部,可以有属性和方法,而外部代码可以通过直接调用实例变量的方法来操作数据,这样,就隐藏了内部的复杂逻辑. 但是,从前面的Student类定义来看,外部代码还是可以自由地修改一个实例的na ...
- Zabbix设置触发器调用远程主机脚本实现触发告警后自动启动自愈功能
参考:https://www.cnblogs.com/xiami-xm/p/8929163.html 当zabbix添加触发器后触发告警后可以设置发送邮件及短信告警,但是恢复故障需要运维人员收到告警以 ...
- Vue Router的导航解析过程
在我没读官方的vue router文档之前,我怎么也没想到路由的解析过程竟然有12步. 12步如下: 导航被触发. 在失活的组件里调用离开守卫beforeRouteLeave . 调用全局的 befo ...