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.
题目是说素数n在radix进制下反转后的数在十进制下也是素数
23是素数,二进制表示10111,反转11101,十进制数是29,纠结死我了
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> bool Isprime(int num) { if(num==2) return true; if(num<2) return false; int temp=(int)sqrt(num*1.0); for( int i=2;i<=temp;i++) if(num%i==0) return false; return true; } int main () { int d,n; while (scanf("%d",&n)&&n>=0) { int revers[100000],i=0,num=0; scanf("%d",&d); if( Isprime(n)) { while( n) { revers[i++]=n%d; n=n/d; //printf("%d",revers[i-1]); } for( int j=0;j<i-1;j++) { num=(revers[j]+num)*d; } num+=revers[i-1]; //printf("\n%d",num); if( Isprime(num)) printf("Yes\n"); else printf("No\n"); } else printf("No\n"); } system("pause"); return 0; }
原文地址:http://blog.csdn.net/lchinam/article/details/43988715