admin 管理员组文章数量: 1087649
PAT A 1015. Reversible Primes
PAT A 1015. Reversible Primes
给一个10进制数
转成D进制
取倒序
再转成10进制
看两个是否都是质数
坑点在于
1不是质数
不考虑这个就有2分得不到
#include <bits/stdc++.h>
using namespace std;
bool isPrime(int x)
{if (x <= 1) return false;for (int i = 2; i * i <= x; i++) {if (x % i == 0) {return false;}}return true;
}
int main()
{//freopen("in", "r", stdin);int N, D;for (;;) {scanf("%d", &N);if (N < 0) break;scanf("%d", &D);vector<int> a;if (!isPrime(N)) {puts("No");continue;}while (N) {a.push_back(N % D);N /= D;}int r = 1;int s = 0;for (auto it = a.rbegin(); it != a.rend(); it++) {int e = (*it);s += (e * r);r *= D;}if (isPrime(s)) {puts("Yes");continue;}puts("No");}
}
本文标签: PAT A 1015 Reversible Primes
版权声明:本文标题:PAT A 1015. Reversible Primes 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1700300107a386476.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论