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 any two positive integers N (<) and D (1), 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

题目大意:

一开始理解错了题意。。。。
数字N在D进制下是不是双重素数。双重素数是本身和倒数皆为素数的数。

实现:判断N是否为素数。如果不是,输出No,否则将该数在D进制下倒过来再化为十进制数,判断是否为素数。如果是,输出Yes,否则输出No.

复习判断素数知识点 注意 ‘ = ’ !!!

#include<bits/stdc++.h>
using namespace std;
bool prime(int x){
    if(x==0||x==1){
        return false;
    }
    if(x==2){
        return true;
    }
    for(int i=2;i<=sqrt(x);i++){//这个地方忘记了=号!!!
        if(x%i==0){
            return false;
        }
    }
    return true;
}
int main()
{
    int a;
    int d;
    while(cin>>a)
    {
        if(a<0){
            break;
        }
        cin>>d;
        //先判断本身是不是素数
        if(!prime(a)){
            cout<<"No"<<endl;
            continue;
        }
        //根据相应地进制转
        string s="";
        int x;
        while(a){
            s+=char(a%d+‘0‘);
            a=a/d;
        }
        //cout<<s<<endl;    

        //反向再把它从d进制转成10进制
        int l = s.length();
        x=0;
        for(int i=0;i<l;i++){
            x=x*d+s[i]-‘0‘;
        }
        //cout<<x<<endl;
        if(prime(x)){
            cout<<"Yes"<<endl;
        }
        else{
            cout<<"No"<<endl;
        }
    }
    return 0;
 } 
 

原文地址:https://www.cnblogs.com/caiyishuai/p/11291829.html

时间: 2024-11-10 14:12:50

PAT 甲级 1015 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[求d进制下的逆][简单]

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

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

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) ——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

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

1027. Colors in Mars (20)【进制转换】——PAT (Advanced Level) Practise

题目信息 1027. Colors in Mars (20) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Re

2014.12.20学习进制转换

进制转换:二进制,八进制,十进制,十六进制. (一)二进制转十进制: 1.写2 2.标指数,从右向左,从0开始依次标记 3.乘系数,一一对应. 4.相加. 例:二进制数1101转十进制数* 1.2   2   2   2 2.2^3   2^2   2^1   2^0 3.1*2^3   1*2^2   0*2^1   1*2^0 4.1*2^3+1*2^2+0*2^1+1*2^0=13 (二)十进制转二进制:除2取余 1.用竖式,对十进制数依次除2,记录每一步余数. 2.一直除到商0为止,从下

顺序栈(进制转换 + 括号匹配 + 判断栈表 + 最长括号匹配长度)

#include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 #define STACK_INIT_SIZE 100 #define STACKLINCREMENT 10