题意:如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做“回文数”。例如,12321就是一个回文数,而77778就不是。当然,回文数的首和尾都应是非零的,因此0220就不是回文数。
事实上,有一些数(如21),在十进制时不是回文数,但在其它进制(如二进制时为10101)时就是回文数。
编一个程序,从文件读入两个十进制数N (1 <= N <= 15)S (0 < S < 10000)然后找出前N个满足大于S且在两种或两种以上进制(二进制至十进制)上是回文数的十进制数,输出到文件上。
本问题的解决方案不需要使用大于32位的整型
输入/出:
SAMPLE INPUT
3 25
SAMPLE OUTPUT
26
27
28
/*
ID:jsntrdy1
PROG: dualpal
LANG: C++
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;
ifstream fin("dualpal.in");
ofstream fout("dualpal.out");
bool isOK(int n,int B)
{
string str="";
while(n)
{
int t=n%B;
str+=t+'0';
n=n/B;
}
int nn=str.length();
for(int i=0;i<=nn/2;i++)
{
if(str[i]!=str[nn-1-i])
return false;
}
return true;
}
bool f(int n)
{
int count=0;//可以表示的进制种数
for(int i=2;i<=10;i++)
{
if(isOK(n,i))
count++;
if(count==2)
return true;
}
return false;
}
int main()
{
int b,n;
fin>>b>>n;
int t=0;
while(t<b)
{
n++;
if(f(n))
{
fout<<n<<endl;
t++;
}
}
return 0;
}