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 ...
随机推荐
- Mysql配置innodb_flush_log_at_trx_commit
当innodb_flush_log_at_trx_commit被 设置为0,日志缓冲每秒一次地被写到日志文件,并且对日志文件做到磁盘操作的刷新,但是在一个事务提交不做任何操作.当这个值为1(默认值)之 ...
- spring boot过滤器FilterRegistrationBean
有2种方式可以实现过滤器 1:通过FilterRegistrationBean实例注册 2:通过@WebFilter注解生效 这里选择第一种,因为第二种不能设置过滤器之间的优先级 为了演示优先级,这里 ...
- web系统基础
网络标准体系架构 B/S(browser/server浏览器)服务器有iis.apache.Tomcat.Ngix.Lighttp等 C/S(client/server客户端)如微信.QQ.Outlo ...
- iOS开发系列-线程同步技术
概述 多线程的本质就是CPU轮流随机分配给每条线程时间片资源执行任务,看起来多条线程同时执行任务. 多条线程同时访问同一块资源,比如操作同一个对象.统一变量.同一个文件,就会引发数据错乱和数据安全的问 ...
- 数组,List,Set相互转化
1.数组转化为List: String[] strArray= new String[]{"Tom", "Bob", "Jane"}; Li ...
- No converter found for return value of type: class com.alibaba.fastjson.JSON解决办法
默认情况下,springMVC的@ResponseBody返回的是String类型,如果返回其他类型则会报错.使用fastjson的情况下,在springmvc.xml配置里加入: <mvc:a ...
- 使用HttpStaus自定义返回状态
一.导入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- [转]C# 之泛型详解
什么是泛型 我们在编写程序时,经常遇到两个模块的功能非常相似,只是一个是处理int数据,另一个是处理string数据,或者其他自定义的数据类型,但我们没有办法,只能分别写多个方法处理每个数据类型,因为 ...
- Python---求100以内的质数
1.首先什么是质数: 一个大于1的正整数,如果除了1和它本身以外,不能被其他正整数整除,就叫质数,也叫素数.如2,3,5,7,11,13,17…. 2.代码如下: 这里做个解析:①Python的for ...
- fiddler抓包工具遇到的问题-------502报错
遇到的问题: 打开浏览器,输入本机的虚拟机地址的bugfree,出现无法连接的提示,具体是: [Fiddler] The connection to '192.168.211.128' failed. ...