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 ...
随机推荐
- [MP3]MP3固件持续分享(2019.1.25)
转载自我的博客:https://blog.ljyngup.com/archives/179.html/ 所有的固件到我的博客就可以下载哦 最后更新于2019.2.1 前言 这篇文章会持续更新不同型号的 ...
- lwip的netif状态管理
netif的状态变化可以设置回调函数, 主要有三项变化, 1 netif up or down,address change,address state change(IPv6) 2 link up ...
- Android Webview H5资源本地化
Android Webview H5资源本地化 一. 创建读取资源项目独立模块 1. 项目依赖的好处 符合模块化的思想,他们相互独立.一个项目持有另一个项目的引用,修改更加方便. (注:compile ...
- ubuntu 14.04 下安装 selenium 2.0
文章参考出处:http://blog.sina.com.cn/s/blog_5042ea610102we4y.html 1.安装 python-pip sudo apt-get install pyt ...
- 基于HttpURLConnection的接口调用,支持GET&POST
单位要做一个多级部署平台,大概意思就是一级系统可以监控下属地域的数据也可管理本地的数据.之前做短信猫用过httpClient请求,与此大同小异.封装了一个两种请求方式的工具类. package com ...
- 安全扫描工具Acunetix即AWVS_13.x系列破解版Linux & Windows
本站所提供工具仅供技术学习交流.请勿用于非法行为.否则后果自负. Acunetix,自动化网络应用安全软件的先驱,已经宣布发布Acunetix第13版.新版本提供了一个改进的用户界面,并引入了创新,如 ...
- 通配符与标签!important的背景展示,也是让我怀疑人生了
是谁在耳边对我说!important提升权重优先级,只为这一句,我用了3600s研究通配符与标签!important的背景展示,也是让我怀疑人生了!选择器权值:标签选择器1,类选择器和伪类选择器:10 ...
- 浅谈centos8与centos7
距离centos8.0(现在已经更新到8.1了)的发布已经过去几个月了,作为一个刚刚接触过几个月centos的萌新来说,本文想通过实际的操作体验来说对比一下centos8代与7代 首先,centos8 ...
- DataX的使用——大数据同步技术
准备工作: 1.视频教学http://113.31.104.47/portal/#/course/dashboard/b34d160db64624732ef152a1118af11a 2.DataX的 ...
- C#中的WinFrom技术实现串口通讯助手(附源码)
C#中的WinFrom技术实现串口通讯助手(附源码) 实现的功能: 1.实现自动加载可用串口. 2.打开串口,并且使用C#状态栏显示串口的状态. 3.实现了串口的接收数据和发送数据功能. 4.串口 ...