PAT甲级——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 (<) and D (1), 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
其实就是将输入的数字转化为该进制的反序列,然后再转化为10进制
然后判断是否是素数即可
#include <iostream>
#include <math.h>
#include <string> using namespace std; bool isPrim(int a)
{
if (a < )
return false;
int b = (int)sqrt(1.0 * a);
for (int i = ; i <= b; ++i)
{
if (a%i == )
return false;
}
return true;
} int getReverNum(int a, int b)
{
string str = "";//得到反转的序列
while (a > )
{
str += a % b + '';
a /= b;
}
int n = ;
for (int i = str.length() - , j = ; i >= ; --i, ++j)
n += (str[i] - '')*pow(b, j);
return n;
} int main()
{
int a, b;
while (cin >> a)
{
if (a < )break;
cin >> b;
if (isPrim(a) && isPrim(getReverNum(a,b)))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return ;
}
PAT甲级——A1015 Reversible Primes的更多相关文章
- 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 (20 分) (进制转换和素数判断(错因为忘了=))
1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "rever ...
- PAT 甲级 1015 Reversible Primes
https://pintia.cn/problem-sets/994805342720868352/problems/994805495863296000 A reversible prime in ...
- PAT A1015 Reversible Primes (20 分)——进制转换,质数
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
- A1015. Reversible Primes
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
- PAT Advanced 1015 Reversible Primes (20) [素数]
题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...
- PAT_A1015#Reversible Primes
Source: PAT A1015 Reversible Primes (20 分) Description: A reversible prime in any number system is a ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642
PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...
随机推荐
- js单线程
由于js是运行在单线程上的,所有浏览器单独开启一个线程来处理事件消息的轮询,避免阻塞js的执行.
- 10 行 Python 代码实现模糊查询/智能提示
10 行 Python 代码实现模糊查询/智能提示 1.导语: 模糊匹配可以算是现代编辑器(如 Eclipse 等各种 IDE)的一个必备特性了,它所做的就是根据用户输入的部分内容,猜测用户想要的 ...
- 记录一次idea因为修改子模块名称而引申的一大堆问题(未完全解决)
文章目录 背景 看图说话 解决 因为这个案例引申出来的错误 修改了之后莫名出现在java 和resource文件后面出现sources root字样 修改了之后java和resource后面出现了[c ...
- SQL SERVER 备份数据库时候错误处理
当备份数据库时候出现如下错误时候 只需要删除备份目标就行了
- CSDN的验证码,真得很糟糕
这是以三种不同的高度来分割各字符 第一张图片是以宽度3来分割,可以看得出,验证码元素保存完好,但 Y 和 9 仍然连在一起 第二张图片是以宽度4来分割,看到了,N已经断了,肉眼虽然仍看得出来是N,但是 ...
- WPF 多语言
1.http://www.cnblogs.com/bear831204/archive/2009/03/17/1414026.html 2.http://www.cnblogs.com/horan/a ...
- 监听事件动态改变dom状态
html代码: <table class="table table-striped"> <thead> <tr> <th>分类ID& ...
- 关于电容与Q值
1, 电容模型 电容阻抗可以表示为: 可算得自谐振频率点为: 在该点,容抗与感抗差为0,电容表现出纯电阻性. 2, 阻抗曲线 自谐点是区分电容器呈容性还是感性的分界点.从阻抗曲线看,在自谐点附近阻抗较 ...
- 面向对象_访问修饰符_构造与析构函数_this指针
1:面向对象 以codeblocks举例,在一个工程里面: File-->new -->Class可以建一个类,可以设置类的参数,是否有set get方法,有无构造函数等设置,.h文件主要 ...
- SpringBoot学习笔记(二):SpringBoot访问静态文件、捕获全局异常、集成Thymeleaf、集成JSP
SpringBoot访问静态文件 什么是静态文件? 不需要通过web容器去得到的文件,直接通过路径就能得到的文件,比如项目的css,js,img等文件. 所有的资源文件都应该在src/main/res ...