PAT A1015Reversible Primes(可逆素数)

主要考察了判断一个10进制数是否为素数(isZS(int ss))和怎么求一个十进制数的n进制数rec(int n,int m)

代码:

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include<iostream>
 4 #include<string>
 5 #include<cmath>
 6 #include<map>
 7 using namespace std;
 8 bool isZS(int s)
 9 {
10     if (s == 2)return true;
11     if (s == 1)return false;
12     if (s == 3)return true;
13     for (int i = 2; i*i <= s; i++)
14     {
15         if (s%i == 0)return false;
16 
17     }
18     return true;
19 }
20 int  rec(int ss,int d)
21 {
22     int apk[1000]; int g = 0;
23     while (ss > 0)
24     {
25         int k = ss%d;
26         apk[g++] = k;
27         ss = ss / d;
28     }
29     int sum = 0;
30     for (int i =0; i<g; i++)
31         sum += apk[i]*pow(d, g-1-i);
32     return sum;
33 }
34 int main() {
35     int n, m;
36     while (1)
37     {
38         cin >> n; if (n < 0)break; cin >> m;
39         if (isZS(n) && isZS(rec(n, m)))cout << "Yes"<<endl;
40         else cout << "No" << endl;
41     }
42     return 0;
43 }

 

posted @ 2018-08-01 13:04  代码拯救世界可以吗  阅读(301)  评论(0编辑  收藏  举报