转换进制&&逆序可以在一起进行,有一点技巧,不要用十进制数来表示低进制,容易溢出。

#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)的更多相关文章

  1. PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...

  2. 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 ...

  3. 【PAT甲级】1015 Reversible Primes (20 分)

    题意: 每次输入两个正整数N,D直到N是负数停止输入(N<1e5,1<D<=10),如果N是一个素数并且将N转化为D进制后逆转再转化为十进制后依然是个素数的话输出Yes,否则输出No ...

  4. 【PAT Advanced Level】1008. Elevator (20)

    没什么难的,简单模拟题 #include <iostream> using namespace std; int main() { int num; cin>>num; int ...

  5. 【PAT Advanced Level】1004. Counting Leaves (30)

    利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...

  6. 【PAT Advanced Level】1006. Sign In and Sign Out (25)

    关键在于清空字符数组和使用scanf进行输入 #include <stdio.h> #include <string.h> #include <fstream> # ...

  7. 【PAT Advanced Level】1014. Waiting in Line (30)

    简单模拟题,注意读懂题意就行 #include <iostream> #include <queue> using namespace std; #define CUSTOME ...

  8. 【PAT Advanced Level】1011. World Cup Betting (20)

    简单模拟题,遍历一遍即可.考察输入输出. #include <iostream> #include <string> #include <stdio.h> #inc ...

  9. 【PAT Advanced Level】1013. Battle Over Cities (25)

    这题给定了一个图,我用DFS的思想,来求出在图中去掉某个点后还剩几个相互独立的区域(连通子图). 在DFS中,每遇到一个未访问的点,则对他进行深搜,把它能访问到的所有点标记为已访问.一共进行了多少次这 ...

随机推荐

  1. DataFrame查增改删

    DataFrame查增改删 查 Read 类list/ndarray数据访问方式 dates = pd.date_range(',periods=10) dates df = pd.DataFrame ...

  2. Linux 帮助 man命令

    man 命令 使用权限 所有用户< /pre> 语法格式 man [[ [-c ] [-t ] [Section] ] | [-k | -f ] ] [-F] [-m] [ -MPath ...

  3. $(this).form("validate") 始终返回false

    onsubmit 提交前触发,返回 false 来阻止提交动作. validate 进行表单字段验证,当全部字段都有效时返回 true .该方法和 validatebox 插件一起使用. 解决:注释掉 ...

  4. frm和ibd恢复sql文件的操作

    情况:有mysql中data文件(仅仅一个数据库) 操作:frm和ibd恢复sql文件的操作 1.创建相同名字的库xxx 2.把ibdata1替换成原来的 3.把数据库xxx内内容全部替换为原来的 完 ...

  5. java写简单Excel 首行是目录 然后前台下载

    页面: <form action="${path}/xxx/xxx.do" method="get" > 表格下载:<input type=& ...

  6. ubuntu下 openvpn客户端的配置

    1.安装openvpn sudo apt-get install openvpn 2.配置openvpn 作为客户端,OpenVPN并没有特定的配置文件,而是由服务器提供方给出一个配置文件.对于认证, ...

  7. DOS中符号的英文对照

    --------------siwuxie095 1..:point 或 dot 或 period 或 full stop 2.空格:space 或 blank ------------------- ...

  8. JFinal ORM和Hibernate简要对比

    1.JFinal采用ActiveRecord实现数据库操作支持,较Hibernate开发效率提升六到十倍. 2.JFinal ActiveRecord较Hibernate学习成本低,一小时内能上手开发 ...

  9. MVC加载部分视图Partial

    加载部分视图的方法:Partial() .RenderPartial() . Action() .RenderAction() . RenderPage() partial 与 RenderParti ...

  10. 如何移除 input type="number" 时浏览器自带的上下箭头?

    Chrome 下 input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { -webkit-appearance: no ...