关闭

PAT 1015 Reversible Primes

标签: PAT 1015Reversible Primes
174人阅读 评论(0) 收藏 举报
分类:

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

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <queue>

using namespace std;
int n,d;
int bin[105];
bool judge(int x)
{
  if(x==1||x==0) return false;
  if(x==2) return true;
  for(int i=2;i*i<=x;i++)
  {
    if(x%i==0)
      return false;
  }
  return true;
}
void fun(int x,int d)
{
  int cnt=0;
  while(x>=d)
  {
    bin[cnt++]=x%d;
    x/=d;
  }
  if(x>0)
    bin[cnt++]=x;
  int num=0;
  for(int i=0;i<cnt;i++)
  {
    num*=d;
    num+=bin[i];
  }
  if(!judge(num))
    printf("No\n");
  else
    printf("Yes\n");
}
int main()
{
  while(scanf("%d",&n)!=EOF)
  {
    if(n<0)
      break;
    scanf("%d",&d);
    if(!judge(n))
    {
      printf("No\n");
      continue;
      
    }
    fun(n,d);
  }
  return 0;

}


0
0
查看评论

浙大PAT 1015题 1015. Reversible Primes

/* 题目的意思是给定数10进制数N和进制D,将N转化成D进制, 再将D进制的数反转,最后转化为10进制数M,判断N和M是否都是质素。 */ #include int isPrime(int n){ int i; if(n<2) return 0; for(i=2;i*i...
  • Sup_Heaven
  • Sup_Heaven
  • 2014-02-04 12:57
  • 2356

PAT 1015 Reversible Primes

1015. Reversible Primes (20) 注意1不是素数 #include using namespace std; int reverse(int n,int d){ int rst=0; while(n){ rst*=d; rst+=n%d; n/=d; ...
  • zhengweijian15
  • zhengweijian15
  • 2014-01-31 12:59
  • 373

pat 1015 Reversible Primes

将这个数 (1)转为D进制数 (2)将这个D进制数反转 (3)将反转后的数再转为十进制数,这个十进制数依然是素数。 这样我们就输出“Yes”。
  • sduuntion
  • sduuntion
  • 2014-11-17 19:51
  • 413

PAT 1015 Reversible Primes (20)

1015. Reversible Primes (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue...
  • u012610237
  • u012610237
  • 2017-03-02 10:06
  • 106

[PAT (Advanced Level) ]1015. Reversible Primes 解题文档

1015. Reversible Primes (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A reversible prime...
  • u010536377
  • u010536377
  • 2015-10-12 20:32
  • 295

1015 Reversible Primes

素数打表,转置判断#include <cstdio> #include <cstring> #include <algorithm> using namespace std;const int MAXN = 1e5+10;bool isprime[MAXN];vo...
  • lhfl911
  • lhfl911
  • 2016-11-28 20:31
  • 165

1015 reversible primes

转。。。不太难,没理解一开始 #include #include class A { public:  void run(); private:  bool isprime(int num);  int reverse(int num,int ...
  • rightwi
  • rightwi
  • 2014-02-22 15:59
  • 303

PAT(Advanced Level) 1015 - Reversible Primes(水题)

*1015. Reversible Primes (20)时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A reversible prime in any number system is ...
  • Snakeflute
  • Snakeflute
  • 2016-12-06 16:38
  • 144

1015 Reversible Primes

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 re...
  • Win_Man
  • Win_Man
  • 2016-02-27 10:03
  • 130

PAT 1015 Reversible Primes(进制转换+素数)

题目https://www.patest.cn/contests/pat-a-practise/1015 题意:若n为素数,且n在某个数字系统中的表示串的倒置也为素数,则称n满足可逆素数。例如,23是素数,且23在2进制下的表示10111的倒置11101即29也为素数,则23是可逆素数。给出十进...
  • lecholin
  • lecholin
  • 2017-05-28 16:49
  • 196
    个人资料
    • 访问:131157次
    • 积分:4659
    • 等级:
    • 排名:第7443名
    • 原创:349篇
    • 转载:0篇
    • 译文:0篇
    • 评论:7条
    最新评论