1015. Reversible Primes (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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 -2Sample Output:
Yes Yes No
解:卡了,醉了,卡1:需要判断n值前后都是质数,第一次我只判断n值逆转后的了,很快就补上了;卡2:判断prime(n)==1&&prime(res)==1,我没注意n值已经换了,不是最初输出的值了,还需仔细。
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int prime(int n)
{
if(n==1) return 0;
if(n==2||n==3||n==5||n==7) return 1;
int k=floor(sqrt(n)+0.5);
for(int i=2;i<=k;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
int main()
{
int n,m;
while(scanf("%d",&n)==1&&n>0)
{
scanf("%d",&m);
int wa=n;
if(prime(n)==0)
{
printf("No
");
continue;
}
int a[20],cnt=0;
while(n)
{
a[cnt++]=n%m;
n=n/m;
}
int res=0,c=0;
for(int i=cnt-1;i>=0;i--)
{
int temp=1;
for(int j=0;j<c;j++) temp*=m;
c++;
res+=a[i]*temp;
}
if(prime(wa)==0||prime(res)==0)
printf("No
");
else
printf("Yes
");
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。