思路:
这个题呢 首先知道怎么转换进制 , 就是一直取余。 然后判断这个数是否为回文数即可
比较简单不做过多解释
java:
import java.util.Scanner;
public class Palindrom_Numbers_1078 {
static int arr[]=new int[1000];
static int b[]=new int[15];
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n;
while((n=s.nextInt())!=0){
int count=0;
for(int i=2;i<=16;i++){
int x=n,t=0;
while(x!=0){
arr[t++]=x%i;
x/=i;
}
if(judge(t))
b[count++]=i;
}
if(count>0){
System.out.print("Number "+n+" is palindrom in basis");
for(int i=0;i<count;i++)
System.out.print(" "+b[i]);
System.out.println();
}else
System.out.println("Number "+n+" is not a palindrom");
}
}
public static boolean judge(int k){
for(int i=0;i<k/2;i++){
if(arr[i]!=arr[k-1-i])return false;
}
return true;
}
}
c++:
#include<iostream>
using namespace std;
int arr[1000];
int b[15];
bool judge(int k){
for(int i=0;i<k/2;i++){
if(arr[i]!=arr[k-1-i])return false;
}
return true;
}
int main(){
int n;
while(cin>>n&&n){
int co=0;
for(int i=2;i<=16;i++){
int x=n;
int t=0;
while(x){
arr[t++]=x%i;
x/=i;
}
if(judge(t))
b[co++]=i;
}
if(co>0){
cout<<"Number "<<n<<" is palindrom in basis";
for(int i=0;i<co;i++){
cout<<" "<<b[i];
}
cout<<endl;
}else
cout<<"Number "<<n<<" is not a palindrom"<<endl;
}
return 0;
}