原题:
Dual Palindromes
A number that reads the same from right to left as when read from left to right is called a palindrome. The number 12321 is a palindrome; the number 77778 is not. Of course, palindromes have neither leading nor trailing zeroes, so 0220 is not a palindrome.
The number 21 (base 10) is not palindrome in base 10, but the number 21 (base 10) is, in fact, a palindrome in base 2 (10101).
Write a program that reads two numbers (expressed in base 10):
N (1 <= N <= 15)
S (0 < S < 10000)
and then finds and prints (in base 10) the first N numbers strictly greater than S that are palindromic when written in two or more number bases (2 <= base <= 10).
Solutions to this problem do not require manipulating integers larger than the standard 32 bits.
题意:
一个数字本身可能不是回文数,但是在其他进制下就可能是回文数了。输入两个数N,S,求出大于S的前N个在2-10进制中有两个或以上进制是回文数的数。
进制转换,没有什么技巧,直接上代码:
/*
ID:newyear111
PROG: dualpal
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include<algorithm>
using namespace std;
const int N=25;
char base[N];
char str[N*100];
void init(){
for(int i=0;i<10;i++){
base[i]=char(i+'0');
}
for(int i=10;i<=20;i++){
base[i]=char('A'+i-10);
}
// cout<<base<<endl;
}
//进制转换函数
int change(int t,int n){
int r,num=0;
while(t){
r=t%n;
t/=n;
str[num++]=base[r];
}
str[num]=0;
return num;
}
//回文数判定函数
bool isOK(int size){
for(int i=0;i<size/2;i++){
if(str[i]!=str[size-i-1])
return false;
}
return true;
}
int main()
{
ifstream fin("dualpal.in");
ofstream fout("dualpal.out");
init();
int n,S,size;
fin>>n>>S;
int i=S+1;
while(n){
int count=0;
for(int j=2;j<=10;j++){
size=change(i,j);
if(isOK(size)){
count++;
}
}
if(count>=2){
n--;
fout<<i<<endl;
}
i++;
}
fin.close();
fout.close();
return 0;
}