zoukankan      html  css  js  c++  java
  • 1015. Reversible Primes (20)

    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<cstdio>
    #include<cmath>
    
    bool isPrime(int n){
        if(n <=1 ) return  false;
        int sqr = (int)sqrt(1.0*n);
        for(int i = 2; i <= sqr; i++){
            if(n%i == 0) return false;
        }
        return true;
    }
    int d[111];
    int main(){
        int n,radix;
        while(scanf("%d",&n) != EOF){
            if(n < 0) break;
            scanf("%d",&radix);
            if(isPrime(n) == false){
                printf("No
    ");
            }else{
                int len = 0;
                while(n != 0){
                    d[len++] = n % radix;
                    n /= radix;
                }
                for(int i = 0; i < len; i++){
                    n = n *radix + d[i]; 
                }
                if(isPrime(n) == true) printf("Yes
    ");
                else printf("No
    ");
                
            }
        }
        return 0;
    }
    //18.8.6 
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    using namespace std; 
    const int maxn = 100100;
    int n,radix,num[maxn];
    
    bool isPrime(int x){
        if(x <= 1) return false;   //测试点2 
        int sqrx = sqrt(x);
        for(int i = 2; i <= sqrx; i++){
            if(x%i == 0) return false;
        }
        return true;
    }
    
    int Reverse(int x,int radix){
        int i = 0,ans = 0;
        do{
            num[i++] = x % radix;
            x /= radix;
        }while(x != 0);
        for(int j = 0; j < i; j++){
            ans = ans*radix + num[j];
        }
        return ans;
    }
    
    int main(){
        while(1){
            memset(num,0,sizeof(num));
            scanf("%d",&n);
            if(n < 0) break;
            else{
                scanf("%d",&radix);
                int u = Reverse(n,radix);
                if(isPrime(n) && isPrime(u)){
                    printf("Yes
    ");
                }else printf("No
    ");
            }
            
        }
        return 0;
    }
  • 相关阅读:
    【转载】sourcetree 克隆项目显示“这是一个无效的源路径”
    【转载】SQL 查询某个数据是否存在
    【转载】windows 静默安装 winpcap
    Linux expect 远程自动登录机器并执行命令
    CentOS 静态编译缺少库文件 /usr/lib/ld: cannot find lxx
    Java常用工具类(随着工作持续更新)
    一些网址笔记
    1 msql的安装和配置
    postgresql 安装和配置
    有管django使用orm 字段报错问题
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/8781781.html
Copyright © 2011-2022 走看看