2017ecjtu-summer training #1 UVA 12050

     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个回文数字

不会写此题啊,膜一下网上大佬代码,没有全看懂,只看懂一部分,没有注释--_--!,他日再破吧。。。

AC代码

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <queue>
#include <vector>
#include <algorithm>
#define maxn 3000
using namespace std;
typedef long long ll;
ll num[maxn];
int n, ans[maxn];

void init()
{
    num[0] = 0, num[1] = num[2] = 9;
    for (int i = 3; i < 20; i += 2)
        num[i] = num[i+1] = num[i-1] * 10;           //预处理i位的回文数的个数,9,9,90,90,900....
}

int main()
{
    init();
    while (scanf("%d", &n) && n)
    {
        int len = 1;
        while (n > num[len])
        {
            n -= num[len];                           //判断第n个回文数字有多少位,len
            len++;
        }
        n--;                                          

        int cnt = len / 2 + 1;                       //写出回文数字后半边
        while (n)
        {
            ans[cnt++] = n % 10;
            n /= 10;
        }
        for (int i = cnt; i <= len; i++)
            ans[i] = 0;
        ans[len]++;

        for (int i = 1; i <= len/2; i++)
            ans[i] = ans[len-i+1];                //前半边由后半边复制过来
        for (int i = 1; i <= len; i++)
            printf("%d", ans[i]);
        printf("\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/stranger-/p/7253192.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值