PAT (Advanced Level) Practice 1015 Reversible Primes (20 分)
2024-09-06 18:20:24
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 ;
}
最新文章
- HTML编辑器
- 查看域名对应的ip地址
- 构建 XCache 的基本步骤
- tomcat源码导入eclipse
- jfinal拦截器301跳转
- xapian安装
- 【python自动化第二篇:python入门】
- 客户端持久化解决方案:indexedDB
- PhoneGap笔记-01 基本使用
- cocos2d-x游戏开发系列教程-超级玛丽07-CMGameMap(五)-地图卷动
- 何为PostgreSQL?
- iOS----KVC和KVO 详解
- C#窗体加载和控件加载不同步导致控件闪烁
- 浏览器本地数据存储解决方案以及cookie的坑
- Python全栈开发之路 【第五篇】:Python基础之函数进阶(装饰器、生成器&;迭代器)
- key-event
- socketserver实现FTP
- lsof and dynamic array in bash/shell
- MySQL锁分类
- Hadoop主要架构