我们可以通过给定的n来判断此回文串的长度,和其与该长度最小的值的差值,我们只需得到回文串的前半部分就可以了。
1位 9
2位 9
3位 9*10
4位 9*10
5位 9*10*10
.。。。。。
例如 n=50 该回文串长度为3 50-18=32,它的前半部分为10+32-1=41 所以该回文串为414
详见代码。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
using namespace std;
typedef long long ll;
const int INF=1e9+10;
const double EPS = 1e-10;
const ll mod=1e9+7;
typedef pair<int,int> P;
int main(){
//freopen("out.txt","w",stdout);
//ios_base::sync_with_stdio(0);
int n;
while(scanf("%d",&n)&&n){
int cnt=1;
ll tmp=1;
for(;;cnt++){
if(cnt&1){
tmp*=1LL*10;
if(cnt==1) tmp--;
if(n-tmp<=0) break;
}else{
if(n-tmp<=0) break;
}
n-=tmp;
}
int cnt1=(cnt+1)/2;
ll sum=1;
for(int i=0;i<cnt1-1;i++)
sum*=10;
sum+=n-1;
printf("%lld",sum );
if(cnt&1)
sum/=10;
while(sum){
printf("%lld",sum%10 );
sum/=10;
}
printf("\n");
}
return 0;
}