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 ...
随机推荐
- Error parsing XML: junk after document element这样的错误 - CSDN博客
很多开发者可能在编写XML布局文件时提示Error parsing XML: junk after document element这样的错误,这里Android123提示大家一般合法的XML文件只有 ...
- Android按钮绑定四种方式
public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCr ...
- java生成验证码并可刷新
手心创建一个简单的页面来显示所创建的验证码 <body> <form action="loginName.mvc" method="post" ...
- Spring - 整合MyBatis
目的: 使用 Spring 容器用单例模式管理 MyBatis 的 sqlSessionFactory : 使用 Spring 管理连接池.数据源等: 将 Dao / Mapper 动态代理对象注入到 ...
- Python文件操作回顾
with open("D:/Temp/a.txt", mode='w', encoding='utf-8') as f: f.write('hello') with open(&q ...
- POJ 3348 /// 凸包+多边形面积
题目大意: 给定的n个点 能圈出的最大范围中 若每50平方米放一头牛 一共能放多少头 求凸包 答案就是 凸包的面积/50 向下取整 /// 求多边形面积// 凹多边形同样适用 因为点积求出的是有向面积 ...
- asp.net Core 获取应用程序所在目录的2种方式
//获取应用程序所在目录的2种方式(绝对,不受工作目录影响,建议采用此方法获取路径).如:d:\Users\xk\Desktop\WebApplication1\WebApplication1\bin ...
- ORM下实现继承的三种方式(TPH TPC TPT)
TPH(Table Per Hierarchy):所有的数据都放在同一个表格内,但是使用辨别标志(Discriminator)的方式来区分 TPC(Table Per Concrete-Type):由 ...
- JS规则 较量较量(比较操作符) 两个操作数通过比较操作符进行比较,得到值为真(true)和假(false)。【>; <; >=; <=; !=;==】
较量较量(比较操作符) 我们先来做道数学题,数学考试成绩中,小明考了90分,小红考了95分,问谁考的分数高? 答: 因为"95 > 90",所以小红考试成绩高. 其中大于号& ...
- 74CMS漏洞打包(从老博客转)
引子 这套CMS是上个月中做的审计,总共找到几个后台漏洞,可后台getshell,一个逻辑漏洞可任意发短信,还有一个前台注入漏洞.不过发到了某平台上之后,审核又要求我提交利用的poc,所以懒得发去了, ...