回文数的判断有个神奇的公式:
g[i]==g[leng+1-i]
其中leng为字符串长度,看每个g[i]是否都满足它,若满足,就是回文数
ps:洛谷的impossible有毒,必须得复制题干中的impossible
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char c[100001];
int t,k,lena,leng=1,a[100001],b[100001],g[100001],n;
void pd();
void jf()
{
int x=0;leng=1;g[1]=0;
while(leng<=lena+1)
{g[leng]=a[leng]+a[lena+1-leng]+x;
x=g[leng]/n;
g[leng]%=n;
leng++;
}g[leng+1]=x;t++;//cout<<t;
while(g[leng]==0&&leng>1)
{leng--;
}
pd();
}
void pd()
{
int o=0;
for(int i=1;i<=leng/2;i++)
{if(g[i]==g[leng+1-i])
{o++;if(o==leng/2)
{cout<<"STEP="<<t;k++;}
}
else
{for(int i=1;i<=leng;i++)
a[i]=g[i];
lena=leng;
}
}
}
int main()
{scanf("%d",&n);
scanf("%s",c);
lena=strlen(c);
for(int i=0;i<=lena-1;i++)
{if(c[i]>57)a[lena-i]=10+c[i]-17-48;
else a[lena-i]=c[i]-48;
}
while(t<=30&&k==0)
{jf();
}
if(k==0)cout<<"Impossible!";
}
来源:oschina
链接:https://my.oschina.net/u/4267179/blog/3620258