不难的一道水题,但是第一次wa了,因为没考虑到字符串末尾要加‘\0’
#include<iostream>
#include<string.h>
using namespace std;
char s1[50],s2[50];
int j;
int fun()
{
int i,k=0;
for(i=j-1;i>=0;i--)
s2[k++]=s1[i];
s2[k]='\0';
if(strcmp(s1,s2)==0)
return 1;
else
return 0;
}
int main()
{
int n;
while(cin>>n)
{
if(n==0)
return 0;
int i,k=0,t;
int a[20];
for(i=2;i<=16;i++)
{
t=n;
j=0;
while(t>0)
{
s1[j++]=t%i;
t=t/i;
}
s1[j]='\0';
if(fun()==1)
{
a[k]=i;
k++;
}
}
if(k==0)
cout<<"Number "<<n<<" is not a palindrom"<<endl;
else
{
cout<<"Number "<<n<<" is palindrom in basis";
for(i=0;i<k;i++)
cout<<" "<<a[i];
cout<<endl;
}
}
return 0;
}