Palindrome Numbers UVA - 12050(第几个回文数)


长度为k的回文串个数有9*10^(k-1)

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 10010, INF = 0x7fffffff;
int num[maxn], a[maxn];
void init()
{
    num[1] = num[2] = 9;
    for(int i=3; i<20; i+=2)
        num[i] = num[i+1] = num[i-1] * 10;
}

int main()
{
    init();
    LL n;
    while(cin>> n && n)
    {
        mem(a, 0);
        int len = 1;
        while(n - num[len] > 0)
        {
            n -= num[len];
            len++;
        }
        n--;
        for(int i=len/2+1; i<=len; i++)
        {
            a[i] = n%10;
            n /= 10;
        }
        a[len]++;
        for(int i=1; i<=len/2; i++)
            a[i] = a[len-i+1];
        for(int i=1; i<=len; i++)
            printf("%d", a[i]);
        printf("\n");

    }
    return 0;
}

 

 
posted @ 2018-07-16 23:31  WTSRUVF  阅读(120)  评论(0编辑  收藏  举报