浙大 PAT Advanced level 1015. Reversible Primes (20)
来源:互联网 发布:131458淘宝卖家工具箱 编辑:程序博客网 时间:2024/04/29 01:35
A 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
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
题目比较简单,主要设计判断一个数是否为素数以及数字的反转。第一次做的时候顾虑太多,处理一组数据输出一个结果即可,不用记录然后统一输出。
思想比较简单,不再重写代码。
/* 10^5(decimal) = 11000011010100000(binary)(共17位)*/#include <iostream>#include <math.h>#include <stack>using namespace std;bool isPrime(int target){double temp = sqrt(target*1.0);if (0 == target || 1 == target){return false;}for (int i = 2; i <= temp; ++i){if (0 == target%i){return false;}}return true;}typedef struct node{int flag;/* 0表示是reversible primes*/struct node *next;}node;int main(){int integer;int radix;stack<int> digit;int value;int temp;node *list = new node;node *pre = list;node *cur;cin >> integer;while (integer >= 0){cur = new node;cur->next = NULL;pre->next = cur;pre = cur;cin >> radix;if (!isPrime(integer)){cur->flag = -1;cin >> integer;continue;}while (radix <= integer){digit.push(integer%radix);integer /= radix;}digit.push(integer);value = 0;temp = 1;while (!digit.empty()){value += digit.top()*temp;temp *= radix;digit.pop();}if (!isPrime(value)){cur->flag = -1;}else{cur->flag = 0;}cin >> integer;}cur = list->next;while (NULL != cur){if (0 == cur->flag){cout << "Yes" << endl;}else{cout << "No" << endl;}pre = cur;cur = cur->next;delete pre;}delete list;system("pause");return 0;}
0 0
- 浙大 PAT Advanced level 1015. Reversible Primes (20)
- 【PAT Advanced Level】1015. Reversible Primes (20)
- 【PAT】【Advanced Level】1015. Reversible Primes (20)
- 1015. Reversible Primes (20) @ PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise —— 1015. Reversible Primes (20)
- 【c++】PAT (Advanced Level)1015. Reversible Primes (20)
- 1015. Reversible Primes (20) ——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1015. Reversible Primes (20)
- PAT (Advanced Level) 1015. Reversible Primes (20) 翻转质数
- PAT (Advanced) 1015. Reversible Primes (20)
- [PAT (Advanced Level) ]1015. Reversible Primes 解题文档
- PAT (Advanced Level) Practise 1015 Reversible Primes (20)
- PAT (Advanced Level) Practise 1015 Reversible Primes (20)
- Pat(Advanced Level)Practice--1015(Reversible Primes)
- PAT(Advanced Level) 1015 - Reversible Primes(水题)
- PAT (Advanced Level) Practise 1015. Reversible Primes (20) 素数筛法 进制转换
- 浙大PAT 1015题 1015. Reversible Primes
- 1015. Reversible Primes (20)-PAT
- Javascript飘窗代码
- cydiaSubstrate hook java/native 研究记录
- WebAPI图片批量上传+修改图片名称
- 当前时间显示
- android测试框架对比(一)
- 浙大 PAT Advanced level 1015. Reversible Primes (20)
- 小型数据的缓存
- eclipse中修改启动tomcat时的jvm参数
- poj1273 Drainage Ditches
- android BroadCast的发送与接收
- Android中intent传递list或者对象的方法
- NPD实现及其与pico一脉相承的关系
- poj 2318(计算几何)
- nodejs express req.body 为undefined的一个bug