zoukankan      html  css  js  c++  java
  • 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个回文串
    思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然后就得到是长度为多少的第几个的回文串了,有个细节注意的是,
    n计算完后要-1
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 typedef long long ll;
     6 using namespace std;
     7 const int maxn = 3000;
     8 ll num[maxn];
     9 int n, ans[maxn];
    10 void init() {
    11     num[0] = 0, num[1] = num[2] = 9;
    12     for (int i = 3; i < 20; i += 2)
    13         num[i] = num[i+1] = num[i-1] * 10;
    14 }
    15 int main() {
    16     init();
    17     while (scanf("%d", &n) && n) {
    18         int len = 1;
    19         while (n > num[len]) {
    20             n -= num[len];
    21             len++;
    22         }
    23         n--;
    24         int cnt = len / 2 + 1;
    25         while (n) {
    26             ans[cnt++] = n % 10;
    27             n /= 10;
    28         }
    29         for (int i = cnt; i <= len; i++)
    30             ans[i] = 0;
    31         ans[len]++;
    32         for (int i = 1; i <= len/2; i++)
    33             ans[i] = ans[len-i+1];
    34         for (int i = 1; i <= len; i++)
    35             printf("%d", ans[i]);
    36         printf("
    ");
    37     }
    38     return 0;
    39 }
     
  • 相关阅读:
    dev c++ 中显示计算机中丢失libiconv2.dll
    poj 1456 Supermarket
    codeforce 867E Buy Low Sell High
    Java编写程序时出现警告:Resource leak: input is never closed 解决方法
    codeforce 8A
    CF1110B
    ZOJ 1914 Arctic Network
    POJ 1258 Agri-Net
    Poj 1751 Highways
    ZOJ 2158 Truck History
  • 原文地址:https://www.cnblogs.com/wydxry/p/7245410.html
Copyright © 2011-2022 走看看