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:
73 10
23 2
23 10
-2
Sample Output:
Yes
Yes
No
没有仔细看好题目,做题有点浮躁(静下心来!)
题目大意:一个数(题目以该数的十进制给出)在对应进制下是一个质数,判断该进制下翻转是否是一个质数
#include <iostream>
#include <cmath>
using namespace std;
bool isprime(int n){
if(n<=1) return false;
for(int i=2;i*i<=n;i++)
if(n%i==0) return false;
return true;
}
int main(){
int n,r;
while(scanf("%d",&n)!=EOF){
if(n<0) break;
scanf("%d",&r);
if(!isprime(n)){//判断当前数是否是是质数
printf("No\n");
continue;
}
int arr[100]={0},len=0;
while(n){//转化成对应进制的数的每一位
arr[len++]=n%r;
n/=r;
}
for(int i=0,j=len-1;i<len;i++,j--){
n=n*r+arr[i];//翻转并计算出十进制数,方便后面判断是否是一个质数
}
printf("%s",isprime(n)?"Yes\n":"No\n");
}
return 0;
}
好好学习,天天向上!
我要考研!