EASY_PAT_ZJU_ADVANCED LEVEL 1015 进制转换 素数

来源:互联网 发布:旅游网站源码 编辑:程序博客网 时间:2024/05/29 13:40

1015. Reversible Primes (20)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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-2
Sample 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
原创粉丝点击