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
题目大意:判断原数字是否是素数,倒序后是否是素数,如果都是素数输出Yse
这里的是指原数字与它在数制下的倒序,如23 2,是10111(2)的倒叙11101的真实数字。
1 #include<stdio.h> 2 #include<math.h> 3 #include<stdlib.h> 4 int in[6]; 5 int main() 6 { 7 int n,d,m,temp; 8 int i,j; 9 int sum,flag; 10 while( scanf("%d",&n)!=EOF) 11 { 12 if( n<0 ) break; 13 scanf("%d",&d); 14 i=0; //分解整数的下标 15 sum=0; //转换后的值 16 flag = 0; //标记是否能转换 17 temp=n; //保存n的值 18 19 while( n ) //分解位数 20 { 21 in[i++]=n%d; 22 n /= d; 23 } 24 for( j=0; j<i ; j++) //求转换的值 25 sum = sum*d+ in[j]; 26 if( isPrime(sum) && isPrime(temp)) //如果转换前和转换后都是素数 27 flag =1; 28 if( !flag) printf("No "); 29 else printf("Yes "); 30 } 31 return 0; 32 } 33 34 int isPrime( int sum) 35 { 36 int i,m; 37 if( sum<2 ) return 0; 38 if( sum==2 || sum ==3 )return 1; 39 m =(int) sqrt(sum)+1; 40 for( i=2; i<m; i++) 41 if( sum%i==0 ) 42 return 0; 43 return 1; 44 }