E - Palindrome Numbers

题目链接:https://vjudge.net/contest/237394#problem/E

A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example, the name “anna” is a palindrome. Numbers can also be palindromes (e.g. 151 or 753357). Additionally numbers can of course be ordered in size. The first few palindrome numbers are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, ... The number 10 is not a palindrome (even though you could write it as 010) but a zero as leading digit is not allowed.
Input The input consists of a series of lines with each line containing one integer value i (1 ≤ i ≤ 2∗109). This integer value i indicates the index of the palindrome number that is to be written to the output, where index 1 stands for the first palindrome number (1), index 2 stands for the second palindrome number (2) and so on. The input is terminated by a line containing ‘0’.
Output
For each line of input (except the last one) exactly one line of output containing a single (decimal) integer value is to be produced. For each input value i the i-th palindrome number is to be written to the output.
Sample Input
1 12 24 0
Sample Output
1 33 151

题目大意:输入n,求第n个回文数,从1开始

个人思路:这题要先找规律,可以发现增长关系是9,9,90,90,900,900····一直下去,这样就可以把要求的数所在的小范围区间求出来,求该数是这个范围内第几个数,然后求出来就行

看代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
#include<map>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=1e8+10;
const int maxk=100+10;
const int maxx=1e4+10;
const ll maxa=2520;
#define INF 0x3f3f3f3f3f3f
ll a[50];
ll ans[25];
void solve(ll n,ll W)
{
    int cnt=W;
  //  cout<<n<<" "<<W<<endl;
    ans[cnt--]=n%10-1;//最后一位是从0开始的,所以要减1,但是这里要注意,n%10可能为0,为0的话其实就是上一位减1,这一位加10
    n=n/10;//同时n减少一位
    if(ans[W]<0)
    {
        ans[W]=ans[W]+10;
        n--;
    }
    while(n)
    {
        ans[cnt--]=n%10;
        n/=10;
    }
    ans[1]++;//注意第一位从1开始的,要++
}
int main()
{
    ios::sync_with_stdio(false);
    ll sum=1,sum1=0,P;
    for(int i=1;i<=50;i+=2)
    {
        a[i]=a[i+1]=9*sum;
        sum*=10;
        sum1+=a[i]*2;
        if(sum1>=2*pow(10,9))
        {
            P=i;
            break;
        }
    }
    ll n;
    a[0]=0;
    while(cin>>n)
    {
        memset(ans,0,sizeof(ans));
        sum=0;
        int W;
        if(n==0)
            break;
        if(n>0&&n<10)
        {
            cout<<n<<endl;
            continue;
        }
        for(int i=1;i<=P;i++)
        {
            sum+=a[i];
            if(n<=sum)
            {
              W=i;
              sum-=a[i];
              n-=sum;
              break;
            }
        }
        //cout<<W<<endl;
        if(W%2==0)
        {
            W=W/2;
            solve(n,W);
            for(int i=1;i<=W;i++)
                cout<<ans[i];
            for(int i=W;i>=1;i--)
                cout<<ans[i];
        }
        else
        {
            W=(W+1)/2;
            solve(n,W);
            for(int i=1;i<=W;i++)
                cout<<ans[i];

            for(int i=W-1;i>=1;i--)
                cout<<ans[i];
        }
        cout<<endl;
       // cout<<n<<endl;
        //cout<<ans<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/caijiaming/p/9370170.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值