首页 > 代码库 > 1015. Reversible Primes (20)
1015. Reversible Primes (20)
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
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 5 6 bool cha(int n) 7 { 8 if(n==1) return false;//注意1不是素数 9 bool temp=true;10 for(int i=2;i<n;i++)11 if(n%i==0)12 {13 temp=false;14 break;15 }16 17 return temp;18 }19 20 int shu[101];21 22 23 int zhuan(int n,int d)24 {25 int count=0;26 int tem=n;27 while(tem)28 {29 shu[count++]=tem%d;30 tem=tem/d;31 }32 33 tem=0;34 int j;35 for( j=0;j<count-1;j++)36 tem=(tem+shu[j])*d;37 tem+=shu[j];38 return tem;39 40 }41 42 43 44 int main()45 {46 int n;47 while(cin>>n)48 {49 if(n<0) break;50 51 int d;52 cin>>d;53 bool ifis=cha(n);54 if(!ifis) cout<<"No"<<endl;55 else56 {57 int rev=zhuan(n,d);58 ifis=cha(rev);59 if(!ifis) cout<<"No"<<endl;60 else cout<<"Yes"<<endl;61 }62 63 }64 return 0;65 }66
1015. Reversible Primes (20)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。