程序代码:
#include <stdio.h>
#define max 100
int i=0;
void dec2yyy(int yyy,int source,char* bin)
{
if(source==0) return;
dec2yyy(yyy,source/yyy,bin);
if((source%yyy)<10) bin[i]=(char)(source%yyy+0x30);
else bin[i]=(char)(source%yyy+0x37);
i++;
}
int main(void)
{
char s[max];
int j=0,k,k1,k2,len=0;
int flag=1;
int flag2=0;
int ss[20];
char* begin=s;
char* tmp=s;
char* end=NULL;
int pan[21]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
for(j=0;j<max;j++) s[j]='\0';
j=0;
while (scanf("%d",&ss[j]))
{
if(ss[j]==0) break;
j++;
}
k=j;
int jj=0;
int p=0;
while(ss[jj]!=0)
{
for(k=2;k<=16;k++)
{
dec2yyy(k,ss[jj],s);
for(k1=0;k1<max;k1++)
{
if(s[k1]=='\0')
{
end=begin+len-1;
break;
}
len++;
}
if(begin==end){flag=1;goto _next;}
while(begin!=end)
{
if((end-begin==1) && (*begin==*end)) {flag=1;break;}
if(*begin!=*end)
{
flag=0;
break;
}
else
{
begin++;
end--;
}
}
_next:
if(flag==1)
{
pan[p++]=k;
flag2++;
}
for(j=0;j<max;j++) {s[j]='\0';i=0;}
len=0;
flag=1;
begin=tmp;
}
if(flag2>0)
{
printf("Number %d is palindrom in basis",ss[jj]);
k2=0;
while(pan[k2]!=0){printf(" %d",pan[k2]);k2++;}
printf("\n");
}
for(k2=0;k2<15;) pan[k2++]=0;
if(flag2<1) printf("Number %d is not a palindrom\n",ss[jj]);
flag2=0;
jj++;
p=0;
}
return 0;
}
[
本帖最后由 wp231957 于 2012-12-7 21:45 编辑 ]