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 0;
  return 1;
}

int main()
{
  int n,d;
  while(scanf("%d",&n)!=EOF)
  {
    if(n<0)            //循环退出的条件
      break;
    scanf("%d",&d);
    if(isPrime(n)==1)      //输入数字是素数
    {
      DI=0;
      do            //【skill】do……while法。转为d进制
      {
        D[DI++]=n%d;
        n/=d;
      }while(n!=0);
      int n2=0;
      for(int i=0 ; i<DI ; ++i)  //生成n逆序的d进制数n2
      {
        n2=n2*d+D[i];      //【warining】n2=千万不要习惯性写成n2+=!!!!!
      }

      if(isPrime(n2)==1)    //也是素数,则Yes
      {
        printf("Yes\n");
        continue;
      }
    }
    printf("No\n");        //转变数不是素数或者输入就不是素数,直接No
  }
  return 0;
}
时间: 2024-09-30 07:25:23

PAT:1015. Reversible Primes (20) AC的相关文章

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) ——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:1027. 打印沙漏(20) AC

#include int main() { int n; char xing; scanf("%d %c",&n,&xing); int num=1,i=1; while(1) { num = num + 2 * (i + 2); //一开始就超出,不改变i if(num > n) break; i += 2; } int MAX=i; for(int k=0 ; kPAT:1027. 打印沙漏(20) AC

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) 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[] { ' '

PAT 甲级 1015 Reversible Primes

https://pintia.cn/problem-sets/994805342720868352/problems/994805495863296000 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 be

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:1004. 成绩排名 (20) AC

#include<stdio.h> #include<stdlib.h> #include<string.h> #include<algorithm> using namespace std; typedef struct STU { char mname[15]; char mID[15]; int mscore; }STU; bool cmp(STU a,STU b) //[skill]使用sort函数自己构造比较 { return a.mscore&g

PAT:1081. Rational Sum (20) AC(类似math.h中的sqrt(1.0*n),algorithm的abs()用于取绝对值)

#include<stdio.h> #include<algorithm> using namespace std; typedef long long ll; //[skill]重命名 struct num { ll zi,mu; //分子分母 }; ll gcd(ll a,ll b) //求最大公约数 { return b==0 ? a:gcd(b,a%b); } num yuefen(num a) //分数约分 { //printf("%lld/%lld.约分为:&