EASY_PAT_ZJU_ADVANCED LEVEL 1015 进制转换 素数
来源:互联网 发布:旅游网站源码 编辑:程序博客网 时间:2024/05/29 13:40
1015. Reversible Primes (20)
时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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 1023 223 10-2Sample Output:
YesYesNo
/************************************************@ AUTHOR: GAOMINQUAN@ DATA: 2014 - 2 - 24@ MAIL: ENSOLEILLY@GMAI.COM@ HARD: EASY **/************************************************/#include<iostream>#include<vector>#include<cmath>using namespace std;vector<int> change_radix(int num,int R){vector<int> newNum;if(num == 0)newNum.push_back(0);else{while(num>0){newNum.push_back(num%R);num /= R;}}return newNum;}int merge_vec_to_reverse_num(vector<int> numbers,int radix){ // cheng vector<int> to reversed real numberint answer = 0;int exp = 0;for(int numI = numbers.size()-1; numI>=0; numI--){answer += numbers[numI] * pow(radix,exp++);}// 利用位数相除的方式,pushBack的数字本来就是反着的,就是说,num[0]是最低位“0",而不是100000中的“1” /* 所以说,如果要计算其原本等于多少,就应该从最低位*pow(R,0++)一直这样下去,所以,如果求逆转,就要从最高位开始。最高的NUM*pow(R,0++)*/return answer;}bool is_primer(int num){int isPrimer = true;if(num == 0 || num == 1){isPrimer = false;}else{for(int i = 2; i<=sqrt(num); i++){ //NOTICE HERE!!!!!!!!!!!!!!!!!!!!if( num % i == 0){isPrimer = false;break;}}}return isPrimer;}int main(){int num = 23, radix = 2;while(true){cin>>num;if(num<0){break;}else{cin>>radix;vector<int> test = change_radix(num,radix);if(is_primer((merge_vec_to_reverse_num(test,radix))) && is_primer(num)){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}}}return 0;}
0 0
- EASY_PAT_ZJU_ADVANCED LEVEL 1015 进制转换 素数
- 1015 进制转换,素数
- PAT (Advanced Level) Practise 1015. Reversible Primes (20) 素数筛法 进制转换
- 筛选素数 进制转换
- PAT 1015 Reversible Primes(进制转换+素数)
- PAT-Advanced Level -1015 判断素数反转后是否是素数
- PAT(Basic Level):数素数
- PAT (Advanced Level) 1027. Colors in Mars (20) 进制转换
- 1027. Colors in Mars (20)【进制转换】——PAT (Advanced Level) Practise
- Basic Level 1007. 素数对猜想 (20)
- Basic Level 1013. 数素数 (20)
- 【PAT 乙级(Basic Level)】数素数
- PAT (Basic Level)1007. 素数对猜想
- PAT (Basic Level)1013. 数素数
- PAT(basic level) 1007 素数对猜想
- PAT(basic level) 1013 数素数(20)
- PAT BASIC LEVEL 1013. 数素数 (20)
- PAT.Basic level.T.1013.数素数
- Java Exception
- sql常见问题
- Cron表达式
- html中各个标签的默认样式汇总
- BUG解决 —— error: can't exec 'codesign' (No such file or directory)
- EASY_PAT_ZJU_ADVANCED LEVEL 1015 进制转换 素数
- Hibernate API解析
- css display:inline-block兼容ie6及火狐
- discuz 添加具有管理员权限的用户
- OUTCAD中视口与视图区别
- ios学习:UINavigationController导航
- Useful webs
- 设置路由器无线上网
- HashMap的使用简介