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 10
23 2
23 10
-2
Sample Output:
Yes
Yes
No
題目大意:
任何數字系統中的可逆素數都是素數,該數字系統中的“反向”也是素數。 例如在十進制系統73中是可逆的素數,因爲它的反面37也是素數。
現在給定任意兩個正整數N(<10 5)和D(1 <D≤10),您應該確定N是否是具有基數D的可逆素數。
輸入規格:
輸入文件包含幾個測試用例。 每個個案佔據一行,該行包含兩個整數N和D。輸入以負N結束。
輸出規格:
對於每個測試用例,如果N是帶底數D的可逆素數,則在一行中打印“是”,否則打印“否”。
樣本輸入:
73 10
23 2
23 10
-2
樣本輸出:
Yes
Yes
No
思路:
理解能力太菜了,讀不懂題目,而且我還是用翻譯軟件 翻譯了題目的!!!我一直以爲用轉換後的進制數來判斷是否爲素數,原來是轉換進制後再逆序,然後再轉回十進制判斷。。。。。。。。
d進制轉換爲十進制
for(int i=0;i<s.size();i++) //d進制轉十進制
shu = shu*d + s[i]-'0';
坑點0: 要判斷1不是素數
坑點3: 判斷素數時i * i <= x ,要寫這個等號
代碼:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
int zhuan(int x,int d)
//轉換d進制 放string裏就已經倒置了,然後轉回十進制
{
string s;
while(x)
{
s += x%d + '0';
x/=d;
}
int shu=0;
for(int i=0;i<s.size();i++) //d進制轉十進制
shu = shu*d + s[i]-'0';
return shu;
}
bool isprime(int x) //判斷素數
{
if(x==1 || x==0) return 0; //測試點0
for(int i=2;i*i<=x;i++) //取等 測試點3
if(x%i==0) return 0;
return 1;
}
int main()
{
int n,d;
while(cin>>n && n>=0)
{
cin>>d;
if(isprime(n) && isprime((zhuan(n,d)))) //都是素數
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}