博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT 1015. Reversible Primes
阅读量:5264 次
发布时间:2019-06-14

本文共 1734 字,大约阅读时间需要 5 分钟。

PAT 1015. 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 (< 105) and D (1 < D <= 10), 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 1023 223 10-2

Sample Output:

YesYesNo

分析

先判断数是否是prime,然后化为radix进制,再反转,再化成十进制判断是不是prime;看了半天才明白什么意思

代码如下

#include
#include
using namespace std;long long int toradix(long long n,long long int radix){ string num; while(n!=0){ num.insert(num.begin(),1,'0'+n%radix); n/=radix; } return stoll(num);}bool isprime(long long int num){ if(num<=1) return false; for(int i=2;i<=sqrt(num);i++) if(num%i==0) return false; return true;}long long int toten(string n,long long int radix){ long long int m=0,tag=0; for(int i=n.size()-1;i>=0;i--) m+=pow(radix,tag++)*(n[i]-'0'); return m;}int main(){ long long int radix,num,n; while(1){ int flag=1; string m,s; cin>>n; if(n<0) return 0; cin>>radix; if(!isprime(n)) flag=0; n=toradix(n,radix); s=to_string(n); for(int i=s.size()-1;i>=0;i--) m.append(1,s[i]); n=toten(m,radix); if(!isprime(n)) flag=0; flag>0?cout<<"Yes"<
<<"No"<

转载于:https://www.cnblogs.com/A-Little-Nut/p/8207060.html

你可能感兴趣的文章
前端性能优化-雅虎军规
查看>>
php--->php 缓冲区 buffer 原理
查看>>
基本数据类型
查看>>
Hybrid APP基础篇(五)->JSBridge实现示例
查看>>
python打印log重复问题
查看>>
开发软件时的复用
查看>>
css清除浮动,最常用的方法
查看>>
UVA 10817 - Headmaster's Headache(三进制状压dp)
查看>>
socket通信中select函数的使用和解释
查看>>
VI 摘要
查看>>
【转载】腾讯云安全组如何开放3306端口让Mysql可访问
查看>>
MySQL性能优化的最佳20+条经验
查看>>
vue学习之router
查看>>
SharePoint2010 skill
查看>>
Eclipse+minGW+Msys 调试 ffmpeg(转)
查看>>
Codeforces Round #155 (Div. 2)
查看>>
HDU 2852 KiKi's K-Number(离线+树状数组)
查看>>
Bicolorings - codeforce
查看>>
脚本和脚本包有什么区别?
查看>>
使用virtualbox 配置 linux host-only虚拟主机连接外网(转载)
查看>>