【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中,每遇到一个未访问的点,则对他进行深搜,把它能访问到的所有点标记为已访问.一共进行了多少次这 ...
随机推荐
- Ansible介绍/安装/入门
http://docs.ansible.com/ansible/ https://galaxy.ansible.com/ Ansible是一个IT自动化工具. 它可以配置系统,部署软件,并编排更先进的 ...
- Segments(叉积)
Segments http://poj.org/problem?id=3304 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- 关于Python的OSError和IOError
参考:http://stackoverflow.com/questions/29347790/difference-between-ioerror-and-oserror 在3.x版本已经移除,剩下O ...
- TZOJ 5280 搜索引擎(模拟字符串)
描述 谷歌.百度等搜索引擎已经成为了互连网中不可或缺的一部分.在本题中,你的任务也是设计一个搜索论文的搜索引擎,当然,本题的要求比起实际的需求要少了许多. 本题的输入将首先给出一系列的论文,对于每篇论 ...
- collections之命名元组
#python中没有为我们提供可命名的tuple的类,这个类需要我们自己来定义,下面我们就自己来定义一个类,然后namedtuple就是可以通过名称来get#tuple中的元素,python中的tup ...
- memcache简单操作
<?php $m = new Memcache(); $m->connect('localhost',11211); //获取版本 echo "server's version: ...
- centos7 二进制安装包安装 mysql5.6
centos7 二进制安装包安装 mysql5.6 一.下载mysql5.6二进制安装包 http://mirrors.sohu.com/mysql/MySQL-5.6/ 如:mysql-5.6.34 ...
- WebBrowser-Javascript与C++互操作
WebBrowser控件是Microsoft提供的一个用于网页浏览的客户端控件,WebBrowser控件的使用相当广泛,例如很多邮件客户端都是使用可编辑的WebBrowser控件作为写邮件的工具,也有 ...
- spring框架之AspectJ的XML方式完成AOP的开发
1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...
- 【每日更新】【SQL实用大杂烩】
11.分页1. select * from (select top 2 * from( select top 3 * from t_table order by field1) a order by ...