PAT Advanced 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 (< 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
题目分析
可翻转质数:十进制中的质数,转换为指定进制并反转后的数字仍然是质数
已知十进制数,和指定进制,判断是否为可翻转质数
解题思路
- 判断输入的十进制数是否为质数
- 将输入的十进制数转换为指定进制数A
- 反转A得到B,判断B是否为质数
易错点
- 题目已知条件中数字为十进制,而不是指定进制下的数,虽然题目中未说明,但是可以从样例中23不可能是2进制判断出
知识点
- 题目没有给出样例数,输入非负数终止,该情况下的接收代码
while(scanf("%d",&n)!=EOF){
if(n<0)break;
}
Code
Code 01
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
bool isPrime(int n) {
if(n<=1)return false;
int sqr=(int)sqrt(1.0*n);
for(int i=2; i<=sqr; i++) {
if(n%i==0)return false;
}
return true;
}
int main(int argc, char * argv[]) {
int n,radix;
while(scanf("%d",&n)!=EOF) {
if(n<0)break;
scanf("%d",&radix);
if(isPrime(n)==false){
printf("No\n");
continue;
}
// 进制转换
// 十进制数转换为指定的radix进制
int index=0,d[111]={0};
do{
d[index++]=n%radix;
n/=radix;
} while(n!=0);
// 逆置的radix进制数转换为十进制数
for(int i=0;i<index;i++){
n=n*radix+d[i];
}
if(isPrime(n))printf("Yes\n");
else printf("No\n");
}
return 0;
}
PAT Advanced 1015 Reversible Primes (20) [素数]的更多相关文章
- 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)-素数
先判断n是否为素数然后把n转化成d进制下再反转,转化为十进制的num判断num是否为素数 注意n为0和1时,不是素数!!!注意反转后的num也有可能为1,不是素数!!! #include <io ...
- 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 甲级 1015 Reversible Primes(20)
1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...
- PAT 1015 Reversible Primes (20分) 谜一般的题目,不就是个进制转换+素数判断
题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...
- PAT (Advanced Level) Practice 1015 Reversible Primes (20 分)
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
- PAT (Advanced Level) 1015. Reversible Primes (20)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- 【PAT Advanced Level】1015. Reversible Primes (20)
转换进制&&逆序可以在一起进行,有一点技巧,不要用十进制数来表示低进制,容易溢出. #include <iostream> #include <vector> ...
- PAT 1015 Reversible Primes (判断素数)
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
随机推荐
- jenkins 最新版 搭建
jenkins 中文网:https://jenkins.io/zh/ 点击下载:https://jenkins.io/zh/download/ 然后选择对应的安装环境,我的CentOS 7.6: 有外 ...
- Python MongoDB 创建数据库
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- Python MySQL 删除表
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- super方法
继承 __init__是python中的构造函数,用于属性的初始化. 如果在子类中定义了构造函数,python默认不会调用父类的构造函数,父类里的属性就不会集成到子类. super 用于调用父类(超类 ...
- 【Android】家庭记账本手机版开发报告五
一.说在前面 昨天 1.添加菜单(查询.清除所有等)2.使用滑动删除 今天 1.创建登入和注册界面 2.向数据库添加一张用户表 问题 做完后在登入时有bug(未解决) 二.界面的搭建 1 ...
- tensorflow--建立一个简单的小网络
In [19]: import tensorflow as tf import numpy as np # #简单的数据形网络 # #定义输入参数 # X=tf.constant( ...
- B站 React教程笔记day1(4)调色板案例
视频地址 main.js import React from "react" import { render } from "react-dom" import ...
- ACM蒟蒻防bug专用 ( •̀ ω •́ )✧
/*********************************************** * _ooOoo_ * * o8888888o * * 88" . "88 * * ...
- java笔记01
java对象数组 Student[] Students = new Student[3]; 与普通数组无差 java集合类 集合类: 面向对象对事物的描述是通过对象来体现的. 为了方便对多个对象进行操 ...
- PAT Advanced 1030 Travel Plan (30) [Dijkstra算法 + DFS,最短路径,边权]
题目 A traveler's map gives the distances between cities along the highways, together with the cost of ...