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 (< 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 10
23 2
23 10
-2
Sample Output:
Yes
Yes
No
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int numReverse(int N, int radix){
int num[], temp, index = , ans = ;
do{
temp = N % radix;
num[index++] = temp;
N = N / radix;
}while(N != );
for(int i = index - , P = ; i >= ; i--){
ans += num[i] * P;
P = P * radix;
}
return ans;
}
int isPrime(int N){
int sqr = (int)sqrt(N * 1.0);
if(N == )
return ;
for(int i = ; i <= sqr; i++){
if(N % i == )
return ;
}
return ;
}
int main(){
int N, D, N2;
while(){
scanf("%d", &N);
if(N < )
break;
scanf("%d", &D);
int temp = numReverse(N, D);
if(isPrime(N) && isPrime(temp))
printf("Yes\n");
else printf("No\n");
}
cin >> N;
return ;
}
总结:
1、判断素数:
int isPrime(int N){
int sqr = (int)sqrt(N * 1.0); //N应该转换为小数
if(N == ) //当N = 1时应返回false,容易忽略,1不是素数
return ;
for(int i = ; i <= sqr; i++){ //i <= sqr; i从2开始查找
if(N % i == )
return ;
}
return ;
}
A1015. Reversible Primes的更多相关文章
- PAT A1015 Reversible Primes (20 分)——进制转换,质数
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
- PAT甲级——A1015 Reversible Primes
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
- PAT_A1015#Reversible Primes
Source: PAT A1015 Reversible Primes (20 分) Description: A reversible prime in any number system is a ...
- PAT 1015 Reversible Primes
1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "rever ...
- 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[求d进制下的逆][简单]
1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...
- PTA (Advanced Level) 1015 Reversible Primes
Reversible Primes A reversible prime in any number system is a prime whose "reverse" in th ...
- pat1015. Reversible Primes (20)
1015. Reversible Primes (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A r ...
- pat 1015 Reversible Primes(20 分)
1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...
随机推荐
- Flutter - AAPT: error: resource android:attr/dialogCornerRadius not found.
Launching lib\main.dart on Nokia X6 in debug mode... FAILURE: Build failed with an exception. * What ...
- [UWP 自定义控件]了解模板化控件(1):基础知识
1.概述 UWP允许开发者通过两种方式创建自定义的控件:UserControl和TemplatedControl(模板化控件).这个主题主要讲述如何创建和理解模板化控件,目标是能理解模板化控件常见的知 ...
- Sql_join left right
1.内连接inner join 只返回两张表中所有满足连接条件的行,即使用比较运算符根据每个表中共有的列的值匹配两个表中的行.(inner关键字是可省略的) ①传统的连接写法: 在FROM子句中列出所 ...
- git bash返回上一级目录
YITU-LIUMZ+Administrator@yitu-liumz MINGW64 ~/learngit/gitskills (dev)$ cd ..\ 注意 cd 后面有空格 然后就会弹出一个 ...
- ssh实现办公室电脑连接家中的电脑
友情提示:如果您不知道您家路由器管理页面的密码,请您忽略此文. 问题背景: 家中有台笔记本电脑,它是通过家中的路由器与外界联网的,这时,我想通过ssh服务让公司的电脑能连上我家中的笔记本. 可以画个图 ...
- C. Multi-Subject Competition
链接 [https://codeforces.com/contest/1082/problem/C] 题意 有n个人,m个科目,每个人都有选的科目si,以及他的能力值ri, 规则是每个科目要么选要么不 ...
- Individual Project复审
复审代码的来源:12061162 王骜 王骜同学的代码注释较多,读起来还是比较容易懂. 代码遵从模块化思想,各个模块之间分工明确,功能重复少,模块之间联系紧密,相互调用明确. 处理单词过程运用了正则表 ...
- 《Linux内核设计与实现》读书笔记 3
第三章 进程管理 3.1进程 概念: 进程:处于执行期的程序.但不仅局限于程序,还包含其他资源(打开的文件,挂起的信号,内核内部数据,处理器状态,一个或多个具有内催音社的内存地址空间及一个或多个执行线 ...
- Linux内核设计与实现 第三章
1. 进程和线程 进程和线程是程序运行时状态,是动态变化的,进程和线程的管理操作都是由内核来实现的. Linux中的进程于Windows相比是很轻量级的,而且不严格区分进程和线程,线程不过是一种特殊的 ...
- 《Linux内核分析与实现》 第五周 读书笔记
第3章 进程管理 20135307张嘉琪 3.1 进程 进程就是处于执行期的程序(目标码存放在某种存储介质上),但进程并不仅仅局限于一段可执行程序代码.通常进程还要包含其他资源,像打开的文件,挂起的信 ...