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 ?rst 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 ?rst 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

时间: 2024-10-27 07:20:48

E - Palindrome Numbers的相关文章

Uva - 12050 Palindrome Numbers【数论】

题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然后就得到是长度为多少的第几个的回文串了,有个细节注意的是, n计算完后要-1! 下面给出AC代码: 1 #include <bits/stdc++.h> 2 typedef long long ll; 3 using namespace std; 4 const int maxn=3010; 5

POJ2402/UVA 12050 Palindrome Numbers 数学思维

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). Additionallynumbers can of course be ordered in size. The ?rst few

zoj2000 Palindrome Numbers

Palindrome Numbers Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

Palindrome Numbers(LA2889)第n个回文数是?

 J - Palindrome Numbers Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 2889 WA了一版面,我也是醉了,就因为一个编译环境不同...... 说多了都是泪. 题目链接:请点击UVALive 2889 转载请注明出处:寻找&星空の孩子 #include<stdio.h> #define LL lo

Leetcode: Palindrome Numbers

尝试用两头分别比较的方法,结果发现无法解决1000021这种问题 1 public class Solution { 2 public boolean isPalindrome(int x) { 3 if(x<0) return false; 4 int lastbit=x%10; 5 int firstbit; 6 int num=x; 7 int bits=0; 8 while(num/10!=0){ 9 num=num/10; 10 bits++; 11 } 12 firstbit=num

UVA 12050 - Palindrome Numbers 模拟

题目大意:给出i,输出第i个镜像数,不能有前导0. 题解:从外层开始模拟 #include <stdio.h> int p(int x) { int sum, i; for(sum=i=1;i<=x;i++) sum *= 10; return sum; } int main() { int n, i, j, t, cs[1000], c; while(~scanf("%d", &n)) { if(n==0) break; i=1; while(n>9*

Palindrome Function

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total Submission(s): 662    Accepted Submission(s): 351 Problem Description As we all know,a palindrome number is the number which reads the same backward as forward

UVA - 12050-Palindrome Numbers

12050 - Palindrome Numbers Time limit: 3.000 seconds 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). Additio

hdu 5062 Beautiful Palindrome Number(Bestcodeer Round #13)

Beautiful Palindrome Number                                                                 Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 116    Accepted Submission(s): 82 Problem Description