EASY_PAT_ZJU_ADVANCED LEVEL 1015 进制转换 素数
2014-02-24 18:00
281 查看
1015. Reversible Primes (20)
时间限制400 ms内存限制32000 kB代码长度限制16000 B判题程序Standard作者CHEN, YueA 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 -2Sample Output:
Yes Yes No
/************************************************@ 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;}
相关文章推荐
- EASY_ZJU_PAT_ADVANCED LEVEL_1027 任意进制转换
- EASY_PAT_ZJU_ADVANCED LEVEL_1011 尽量用Vector代替Array,这样会方便很多
- EASY_ZJU_PAT_Advanced Level_1046 寻找最多子串
- EASY_ZJU_PAT_ADVANCED LEVEL_1023 大数乘法
- EASY_ZJU_PAT_ADVANCED LEVEL 1050 高效输入含有空格的字符串 高效删除字符串中的字符
- EASY_ZJU_PAT_ADVANCED LEVEL 1005
- PAT-Advanced Level -1015 判断素数反转后是否是素数
- EASY_PAT_ZJU_1046 求循环数字公路中两个出口的最短距离
- zju pat 1015
- EASY_PAT_ADVANCED LEVEL 1008_简单的面向对象 队列的使用
- zju pat 1015
- 1007. 素数对猜想 (20) (数学啊 ZJU_PAT)
- 1013. 数素数 (20) (数学啊 ZJU_PAT)
- EASY_PAT_ADVANCED LEVEL 1058 A+B IN HOGWART 不同进制数字的加法运算
- EASY_ZJU_PAT ADVANCED LEVEL_1031 仔细分析,删繁就简
- ZJU PAT 1015 德才论
- 1015. 德才论 (25) (结构体啊 ZJU_PAT)
- PAT 1015 Reversible Primes(进制转换+素数)
- EASY_ZJU_PAT_1019_General Palindromic Number_数值转换
- PAT_ZJU_ADVANCED LEVEL_ 1042_ 简单过程模拟