pat1015. Reversible Primes (20)

1015. Reversible Primes (20)

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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 <cstdio>
 2 #include <cstring>
 3 #include <string>
 4 #include <queue>
 5 #include <stack>
 6 #include <iostream>
 7 using namespace std;
 8 bool prime[100005];
 9 void getprime(int n){
10     memset(prime,false,sizeof(prime));
11     int i,j;
12     prime[2]=true;
13     for(i=3;i<=n;i+=2){
14         prime[i]=true;
15         for(j=3;j*j<=i;j++){
16             if(i%j==0){
17                 prime[i]=false;
18                 break;
19             }
20         }
21     }
22 }
23 int main(){
24     //freopen("D:\\INPUT.txt","r",stdin);
25     getprime(100005);
26     int n,d;
27     while(scanf("%d",&n)!=EOF){
28         if(n<0){
29             break;
30         }
31         scanf("%d",&d);
32         if(!prime[n]){
33             printf("No\n");
34             continue;
35         }
36         queue<int> q;
37         while(n){
38             q.push(n%d);
39             n/=d;
40         }
41         //cout<<n<<endl;
42         while(!q.empty()){
43             n*=d;
44             n+=q.front();
45             q.pop();
46         }
47         //cout<<n<<endl;
48         if(prime[n]){
49             printf("Yes\n");
50         }
51         else{
52             printf("No\n");
53         }
54     }
55     return 0;
56 }
时间: 2024-10-10 21:04:11

pat1015. Reversible Primes (20)的相关文章

PAT-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 (&

PAT-1015 Reversible Primes (20)

#include<stdio.h> #include<cmath> #include<algorithm> #include<stack> using namespace std; int main() { int n,d; stack<int> s; //freopen("1015-in.txt","r",stdin); //freopen("1015-out.txt","w

1015. Reversible Primes (20) ——PAT (Advanced Level) Practise

题目信息: 1015. Reversible Primes (20) 时间限制 400 ms 内存限制 32000 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

PAT 甲级 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

1015 Reversible Primes (20)(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 (&

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 (&

1015. Reversible Primes (20) C#实现

1 static void Main(string[] args) 2 { 3 string[] str; 4 bool FLAG; 5 int n = 0, d = 0; 6 List<int> N = new List<int>(); 7 List<int> D = new List<int>(); 8 9 do 10 { 11 FLAG = true; 12 str = Console.ReadLine().Split(new Char[] { ' '

PAT1015. Reversible Primes

//题的理解是n在基数b中的的表示中,正序和逆序值都是素数,但是其实可直接判断n,因为n在b中的正常序列值就是再换成十进制就是n,但是不A:不知道为什么 用笨方法,先把n展开成b进制,正常计算其实是翻转值,倒序是正常序列值,两者都是素数时,yes,否则no,A了 也可以用atoi或者itoa但是本地可以,linix系统不支持这两个c函数,可以用c++里的string试试,直接把字符串变成数值貌似可以,小白还没解锁该技巧: #include<cstdio>#include<cmath>

PAT:1015. Reversible Primes (20) AC

#include<stdio.h> #include<math.h> #include<algorithm> using namespace std; int D[111]; //存放拆解的数字 int DI=0; //D的数组下标 bool isPrime(int n) { if(n<=1) return 0; int sqr=(int)sqrt(1.0*n); for(int i=2 ; i<sqr+1 ; ++i) if(0==n%i) return