栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

A1015 Reversible Primes (20 分)PAT甲级真题(C++)满分题解【进制转换+素数判断】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

A1015 Reversible Primes (20 分)PAT甲级真题(C++)满分题解【进制转换+素数判断】

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

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
#include
using namespace std;
bool isprime(int n)
{
    if(n<=1)return false;
    int mid=int(sqrt(n));
    for(int i=2;i<=mid;i++)
    {
        if(n%i==0)
            return false;
    }
    return true;
}
int main()
{
    int n,d;
    while(true)
    {
        scanf("%d",&n);
        if(n<0)
            break;
        scanf("%d",&d);
        if(!isprime(n)){
            printf("%sn","No");
            continue;
        }
        int index=0,convert[100000];
        do{
            convert[index++]=n%d;
            n=n/d;
        }while(n!=0);
        int reverse=0;
        for(int i=0;i 
运行结果如下: 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/779431.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号