pat 甲级 1015 Reversible Primes

不难
但是令我十分迷茫的一道题

要注意理解好题意,进制翻转的意思是把这个数先转换成d进制,再在d进制中翻转,然后再转化成10进制,判断它是不是素数。

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

int isprime(int a){

    for(int i=2; i<=sqrt(a); i++){
        if(a%i==0) return 0;
    }
    return 1;
    //for(int i=2; i*i<=a; i++){
        if(a%i==0) return 0;
    }
    return 1;
}

其次就是我还没明白的一点,,
这道题中有测试点是关于 <= 1 的数
但是我不在isprime()函数里判断,放在主函数里判断,这个点就是过不了,,
没想明白,,
迷茫,害怕,,

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int n, d;
bool key;

int isprime(int a){
    if(n <= 1) return 0;
    for(int i=2; i*i<=a; i++){
        if(a%i==0) return 0;
    }
    return 1;
}


int main(){

    key = true;
    while(key){

        scanf("%d",&n);
        if(n < 0) break;
        else {
            scanf("%d",&d);
            /*if(n <= 1) {      //不通过,why?
                cout << "No" << endl;
                continue;
            }*/
            /*if(isprime(n)==0 || n<=1) { //这样也不行
                cout << "No" << endl;
                continue;
            }*/
            if(isprime(n)==0) {
                cout << "No" << endl;
                continue;
            }
            int arr[11000], l=0;
            do{
                arr[l++] = n%d;
                n /= d;
            }while(n!=0);
            for(int i=0; i<l; i++){
                n = n*d + arr[i];
            }           
            if(isprime(n)==1) cout << "Yes" << endl;
            else cout << "No" << endl;

        }
    }

    return 0;
}

牙依旧很疼,,,

猜你喜欢

转载自blog.csdn.net/mdzz_z/article/details/81334241
今日推荐