1 题目
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 (< 10^5^) 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
题目链接 https://pintia.cn/problem-sets/994805342720868352/problems/994805495863296000
2 解题思路
给出一个数N和一个基数D,要求在十进制下,N是个素数,且在D进制下的数值倒过来,再反转成十进制,还是个素数。先做素数判断,再做进制转换,然后再做逆序,素数判断,这道题就结束了。但是需要注意的是,1不是素数。之前没有意识到这一点,结果被卡在了第二个测试点卡了好久。
可以用一个stack来记录中间过程,易于操作。
3 AC代码
/*
** @Brief:No.1015 of PAT advanced level.
** @Author:Jason.Lee
** @Date:2018-7-18
*/
#include<ios