您的位置:首页 > 其它

PAT--1015. Reversible Primes

2016-07-16 20:09 429 查看
pat 1015

题解

判断N是否为素数;

N转换为D进制下的数,reverse之后再转换为十进制,判断是否为素数。

#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;

bool isPrime(int n){

if(n < 2) return false;

int k = sqrt(n);
for(int i = 2; i <= k; ++i){
if(n % i == 0) return false;
}
return true;
}

bool solve(int N, int D){

if(!isPrime(N)) return false;

string n;
while(N){
n += N % D + '0';
N /= D;
}
int res = 0;
for(int i = 0; i < n.length(); ++i){
res = res * D + (n[i] - '0');
}

return isPrime(res);
}

int main(){

int N, D;
while(cin >> N && N >= 0){
cin >> D;
cout << (solve(N, D) ? "Yes" : "No") << endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pat