码迷,mamicode.com
首页 > 其他好文 > 详细

1015. Reversible Primes (20)

时间:2015-01-30 07:52:34      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

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   else
56   {
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  
View Code

 

 
 
技术分享
 
 
 
技术分享
技术分享技术分享技术分享技术分享技术分享技术分享

1015. Reversible Primes (20)

标签:

原文地址:http://www.cnblogs.com/xiaoyesoso/p/4261441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!