PAT (Advanced Level) Practice 1015 Reversible Primes (20 分)
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
思路:先判断自身是否是素数,再转换为对应进制的数,再判断是否是素数。
#include <iostream>
#include <string.h>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <stack>
using namespace std;
int n,d;
int _prime(int x)
{
if(x==) return ;
int flag=;
for(int i=;i*i<=x;i++){
if(x%i==){
flag=;
break;
}
}
return flag;
}
int _deal(int n,int d)
{
stack<int> s;
while(n){
s.push(n%d);
n/=d;
}
int sum=,t=;
while(!s.empty()){
sum+=s.top()*t;
t*=d;
s.pop();
}
return sum;
}
int main()
{
while(cin>>n&&n>){
cin>>d;
int flag;
if(!_prime(n)) flag=;
else{
if(_prime(_deal(n,d))) flag=;
else flag=;
}
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return ;
}
PAT (Advanced Level) Practice 1015 Reversible Primes (20 分)的更多相关文章
- PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642
PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...
- 【PAT Advanced Level】1015. Reversible Primes (20)
转换进制&&逆序可以在一起进行,有一点技巧,不要用十进制数来表示低进制,容易溢出. #include <iostream> #include <vector> ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...
- PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642
PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...
- PAT (Advanced Level) Practice 1152 Google Recruitment (20 分)
In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...
- PAT (Advanced Level) Practice 1120 Friend Numbers (20 分) (set)
Two integers are called "friend numbers" if they share the same sum of their digits, and t ...
- 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 (20分) 谜一般的题目,不就是个进制转换+素数判断
题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...
随机推荐
- Sklearn--(SVR)Regression学习笔记
今天介绍一个机器学习包,sklearn.其功能模块有regression\classification\clustering\Dimensionality reduction\data preproc ...
- Import This - The Zen of Python
The Zen of Python -- by Tim Peters Beautiful is better than ugly.Explicit is better than implicit.Si ...
- mysql ---- Host '' is not allowed to connect to this MySQL server
mysql>use mysql mysql>update user set host= '%' where user = 'root'; 此时如果提示报错,不用管,继续往下走 select ...
- nginx官网版本说明
nginx软件下载:http://nginx.org/en/download.html Mainline version:Nginx 正在主力开发的版本Stable version:最新稳定版,生产环 ...
- 算法将一个对象中的某一个key值变为true,其他值都为false
主要运用在,v-if v-show切换不同内容时,非常快的打开某一个区域,关闭其他的区域哈. 这样就不需要每一个设置false,打开区域设置为true. 可以优化代码哈 for in 主要循环对象(空 ...
- sublime笔记
插件安装和使用 首先,要安装package control,按照官方方法安装: https://packagecontrol.io/installation 重启Sublime Text 3. 如果在 ...
- leetcode--js--Longest Substring Without Repeating Characters
问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...
- java8 常用代码
1. 使用java8 提取出 list 中 bean 的某一属性 public static void main(String[] args) { List<Student> stuLis ...
- xPath和html基础扫盲
xPath:一种HTML和XML的查询语言,他能在XML和HTML的树状结构中寻找节点 安装xPath: pip方法: pip install lxml win+R:cmd 打开命令控制台: 此 ...
- java Reflection(反射)基础知识讲解
原文链接:小ben马的java Reflection(反射)基础知识讲解 1.获取Class对象的方式 1.1)使用 "Class#forName" public static C ...