题目

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 reversible prime in any number system is a prime whose "reverse" in that number system is also a prime.

就是说,可逆素数: 在十进制下,N是个素数,且在D进制下的数值倒过来后,再转成十进制,还是个素数

反正我是真的从这个英文中看不出来这个意思。

题目就是:给出 N, 进制 D,输出 N 是否是 可逆素数。

思路

  • 编写判断十进制数是否是素数的方法(for循环进行到它开根号就可以了)
	    for (int i = 2; i <= sqr; ++i)
if (n % i == 0) return false;
  • 先判断 N 是否是 素数,若不是,直接输出 No 结束。

  • 求得 ND 进制下的反转后的结果,用一个数组保存。

    比如 123 在十进制下 反转后是 321(就算只有1位或者是0也要进行一次,所以用 do while

    • n 对10取余得到3,3写进数组第一个位置;
    • n = n / 10 = 12;
    • 重复上面的步骤
  • 把反转后的数字转成十进制再判断是否是素数

	// 转成十进制
for (int i = 0; i < len; ++i)
n = n * radix + arr[i];

代码

只要明白题目意思,也就没有什么难度。

#include <iostream>
#include <cmath>
using namespace std;
/**
* 这个题理解题目意思很重要
* A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime.
* 就是说,可逆素数: 在十进制下,N是个素数,且在D进制下的数值倒过来,再反转成十进制,还是个素数
* 反正我是真没看出来
*/ // 是否是素数
bool isprime(int n) {
if (n <= 1) return false;
int sqr = sqrt(n);
for (int i = 2; i <= sqr; ++i)
if (n % i == 0) return false;
return true;
} int main() {
// 一个十进制的n,一个给定的进制D
int n, radix;
while (cin >> n) {
// 题意,最后一行只有一个负数,表示输入结束
if (n < 0) break;
cin >> radix;
// 首先n在十进制下要是个素数
if (!isprime(n)) {
cout<< "No" << endl;
continue;
}
// 得到在D进制下反转后的编码,如123在十进制下反转后是321
int len = 0, arr[20] = {0};
do {
arr[len++] = n % radix;
n = n / radix;
}while (n > 0);
// 转成十进制
for (int i = 0; i < len; ++i)
n = n * radix + arr[i];
// 如果转换之后的数字还是素数,那么它就是 可逆素数
cout<< (isprime(n) ? "Yes" : "No") << endl;
} return 0;
}

PAT 1015 Reversible Primes (20分) 谜一般的题目,不就是个进制转换+素数判断的更多相关文章

  1. PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...

  2. PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

    1015 Reversible Primes (20 分)   A reversible prime in any number system is a prime whose "rever ...

  3. 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 ...

  4. 1015 Reversible Primes (20 分)

    A reversible prime in any number system is a prime whose "reverse" in that number system i ...

  5. 【PAT甲级】1015 Reversible Primes (20 分)

    题意: 每次输入两个正整数N,D直到N是负数停止输入(N<1e5,1<D<=10),如果N是一个素数并且将N转化为D进制后逆转再转化为十进制后依然是个素数的话输出Yes,否则输出No ...

  6. PAT 1015 Reversible Primes[求d进制下的逆][简单]

    1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...

  7. pat 1015 Reversible Primes(20 分)

    1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...

  8. PAT Advanced 1015 Reversible Primes (20) [素数]

    题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...

  9. PAT 1015 Reversible Primes

    1015 Reversible Primes (20 分)   A reversible prime in any number system is a prime whose "rever ...

随机推荐

  1. Vue 3.0 Composition API - 中文翻译

    Composition API 发布转载请附原文链接 https://www.cnblogs.com/zgh-blog/articles/composition_api.html 这两天初步了解了下 ...

  2. 探索ORACLE之ASM概念(完整版)

    探索ORACLE之ASM概念(完整版) 本文出自https://www.jb51.net/article/43527.htm ASM是Oracle 10g R2中为了简化Oracle数据库的管理而推出 ...

  3. CentOS 7.4 安装网易云音乐

    1.下包–>网易云音乐 Ubuntu14.04(推荐14.04依赖包网上能找到) 提示:16.04有部分依赖包还找不到,有兴趣可以自行打包RPM安装. 2.解包 (1)使用 ar -vx解压ub ...

  4. Windows环境,获取当前线程的ID,GetCurrentThreadId

    GetCurrentThreadId 打印格式:0x%08lx 头文件:processthreadsapi.h (include Windows Server 2003, Windows Vista, ...

  5. SpringBoot应用操作Rabbitmq(topic交换器高级操作)

    一.topic交换器为主题交换器,可以根据路由key模糊匹配 实现模型图 二.实战 1.引入maven <dependency> <groupId>org.springfram ...

  6. 团队一致性的PHP开发环境之Docker

    docker php环境模型 docker 简介 Docker 是一个开源的应用容器引擎 让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现 ...

  7. Tomcat 8 Host-Manager配置访问的方法,全网唯一正确配置

    2019独角兽企业重金招聘Python工程师标准>>> 环境: 操作系统:         Linux version 2.6.32-696.10.1.el6.x86_64 (moc ...

  8. 学数据库你竟然不用用JAVA写代码,可惜你遇到了我! JAVA连接数据库(JDBC)的安装使用教程

    Step 1 你得有Eclipse 没有出门右拐,我教不了你. Step 2 你得有Mysql MySQL的详细安装过程,我在另一篇博客中给出.戳我 Step 3 安装JDBC 可以去官网下,如果用的 ...

  9. 数学--数论--HDU 2802 F(N) 公式推导或矩阵快速幂

    Giving the N, can you tell me the answer of F(N)? Input Each test case contains a single integer N(1 ...

  10. P5522 [yLOI2019] 棠梨煎雪

    updata on 2020.3.19 今天把博客从洛谷往博客园搬,图炸了 其实早就发现了,懒得管 那图其实就是一个用dev自带的调试功能调试时,RE了的报错 当时觉得很奇怪看不出是啥,现在再看已经觉 ...