Reversible Primes(c++)

Reversible Primes

  • 思路

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 (<10​5​​) and D (1 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

思路

一开始的时候都没读懂题目什么意思,又是翻译又是看解题才懂。o(╥﹏╥)o
原来还要转进制的!!首先判断N是不是素数,如果是,就用N转为D进制的逆序再转为十进制,判断这个数是不是素数。
有点绕QwQ

这个转进制和数据逆序。。。

还是自己太垃圾了。
首先,逆序,例如23 2这个样例,一般手算都是,23除以2商11余1,11除以2商5余1,5除以2商2余1,2除以2商1余0,1除以2商0余1.转为2进制为10111(从后往前取余数)。
逆序我们可以直接从前往后取:

string res;
	while(N){//逆序 
		res += N%D + '0';
		N /= D;
	}

res就是D进制逆序。
然后转为十进制:

int st = 0;
	for(int i=0;i

最后判断st是不是素数。

代码如下:

#include
#include
using namespace std;

//判断是否为素数 10进制 
bool sushu(int N){
	if(N<=1)return false;
	int k = 0;
	for(int i=2;i>N;
	while(N > 0){
		cin>>D;
		if(sushu(N)&&sushu(zhuan(N,D))){
			cout<<"Yes"<>N;
	}
	
	return 0;
}

唉,垃圾如我,越来越感觉自己菜了。

但是::::::

一个集坚强与自信于一身的菇凉。

你可能感兴趣的:(Reversible Primes(c++))