您的位置:首页 > 编程语言 > C语言/C++

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
-2
Sample 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;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ OJ ZJU PAT