十进制转任意进制
假设十进制数为number,转换的进制为digits,则将numbers%digits(根据余数的情况做相应处理)
结果保存在字符串str中,将numbers变为numbers/digits;直到numbers为零。
得到的结果为逆序,需要将其倒转,倒转后即为所求。
这里不需要倒序,直接转
Sample Input:
73 10 23 2 23 10 -2
Sample Output:
Yes Yes No
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <math.h>
using namespace std;
bool Prime(int n)
{
if(n == 1 || n == 0)
{
return false;
}
for(int i=2;i<=sqrt((double)n);i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}
int reverse(int n,int radix)
{
int total=0;
while(n)
{
total = total*radix + n%radix;
n = n/radix;
}
return total;
}
int main()
{
int num;
int radix;
vector<string> V;
while(1)
{
cin>>num;
if(num<0)
break;
cin>>radix;
if(Prime(num) && Prime(reverse(num,radix)))
{
V.push_back("Yes");
}
else
{
V.push_back("No");
}
}
for (int i = 0; i < V.size(); i++)
{
cout<<V[i]<<endl;
}
}