1015 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:
1 | 73 10 |
Sample Output:
1 | Yes |
题意
判断一个素数是不是可逆素数,这个题目稍微理解困难一点点的是,它并不是直接给你一个37,让你判断将它反转之后的73是不是素数,还有一个D,这个radix D是指基数,你也可以理解为进制,就是你要将N转换成D进制数,反转,再转换成10进制数,然后看它是不是可逆素数.
这道题的另一个让我困惑的点是,原来并不是让它输完了数据之后,再显示结果,而是直接输入完一行,就显示一行,所以上面那个例子,在程序里面,应该是:
1 | 73 10 |
这样子的.
思路
这里我们为了让思维更加规整,先创建两个工具函数,一个用来判断某个数字是不是素数:
1 | public static bool IsPrime(int input) |
另一个函数用来反转素数:
1 | public static int Reverse(int prime, int radix) |
然后我们只要获取到输入的数据,进行判断就行了.
代码
1 | public static void Main() |