【PAT Advanced Level】1015. Reversible Primes (20)
转换进制&&逆序可以在一起进行,有一点技巧,不要用十进制数来表示低进制,容易溢出。
#include <iostream>
#include <vector>
using namespace std; bool isPrime(int n)
{
if(n < 2)
return false;
if(n == 2)
return true;
if(n % 2 == 0)
return false;
for(int i = 3; i < n; i += 2)
{
if(n % i == 0)
return false;
}
return true;
} int reverseRadix(int n, int d)
{
vector<int> v;
int reverse = n;
while (reverse != 0)
{
int tmp = reverse % d;
if(tmp != 0)
v.push_back(tmp);
for(int i = 0; i < v.size(); i++)
v[i] *= d;
reverse /= d;
}
int result = 0;
for(int i = 0; i < v.size(); i++)
result += v[i] / d;
return result;
} int main()
{
int n, d;
while (cin>>n)
{
if(n < 0)
break;
cin>>d;
int r = reverseRadix(n, d);
if(isPrime(r) && isPrime(n))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
【PAT Advanced Level】1015. Reversible Primes (20)的更多相关文章
- 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 ...
- 【PAT Advanced Level】1008. Elevator (20)
没什么难的,简单模拟题 #include <iostream> using namespace std; int main() { int num; cin>>num; int ...
- 【PAT Advanced Level】1004. Counting Leaves (30)
利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...
- 【PAT Advanced Level】1006. Sign In and Sign Out (25)
关键在于清空字符数组和使用scanf进行输入 #include <stdio.h> #include <string.h> #include <fstream> # ...
- 【PAT Advanced Level】1014. Waiting in Line (30)
简单模拟题,注意读懂题意就行 #include <iostream> #include <queue> using namespace std; #define CUSTOME ...
- 【PAT Advanced Level】1011. World Cup Betting (20)
简单模拟题,遍历一遍即可.考察输入输出. #include <iostream> #include <string> #include <stdio.h> #inc ...
- 【PAT Advanced Level】1013. Battle Over Cities (25)
这题给定了一个图,我用DFS的思想,来求出在图中去掉某个点后还剩几个相互独立的区域(连通子图). 在DFS中,每遇到一个未访问的点,则对他进行深搜,把它能访问到的所有点标记为已访问.一共进行了多少次这 ...
随机推荐
- WinForm多线程编程与Control.Invoke的应用浅谈
在WinForm开发中,我们通常不希望当窗体上点了某个按钮执行某个业务的时候,窗体就被卡死了,直到该业务执行完毕后才缓过来.一个最直接的方法便是使用多线程.多线程编程的方式在WinForm开发中必不可 ...
- 关于EL表达式取值的问题
EL表达式取值时,如果没有指定作用域,EL表达式会自动按照作用域的大小,从小到大依次去找;比如${s},会自动按照"pageContext,request,session,applicati ...
- Mysql修改密码办法
方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:my ...
- 网站连接数据库连接不上原因是ip地址与端口号格式不对
192.168.1.1:8080这样连接一直出错, 后来改为192.168.1.1,8080就可以了 原因是格式不对,把冒号给为逗号就可以了
- python while语句写法
count=5 while count>0: print 'i love python' count=count-1 else: print 'over'
- [VBScript] 自动删除2小时以前生成的文件
保存为deleteTempFiles.vbs,双击即可运行 dim folder, file, mFSO, subfolder Set mFSO = CreateObject("Script ...
- c++11 stl 学习之 pair
pair以模板的方式存储两个数据 namespace std {template <typename T1, typename T2>struct pair {// memberT1 fi ...
- 8.14 右键自定义菜单 更加iframe 内容高度调整 iframe高度 js定时
<div class="main_contain" id="z_div" style="position: relative;"> ...
- 06 数据库入门学习-视图、sql注入、事务、存储过程
一.视图 1.什么是视图 视图本质是一张虚拟的表 2.为什么要用 为了原表的安全 只要有两大功能 1.隐藏部分数据,开放指定数据 2.视图可以将查询结果保存,减少sql语句的次数 特点: 1.视图使用 ...
- 解决Jedis链接报超时异常和connection reset异常的方法
一.链接池配置 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" ...