Uvs-12050 palindrome numbers

Source: Internet
Author: User

Description

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.151Or753357). 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 number10Is not a palindrome (even though you cocould write it010) 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 IIndicates the index of the palindrome number that is to be written to the output, where index 1Stands for the first palindrome number (1), Index 2Stands for the second palindrome number (2)And so on. The input is terminated by a line containing 0. Output for each line of input (partition t the last one) exactly one line of output containing a single (decimal) integer value is to be produced. For each input value IThe I-thPalindrome number is to be written to the output. sample input
112240
Sample output
133151 meaning: Find the n-th string of thinking: First you can know that the length of K of the number of return strings 9*10 ^ (k-1), then calculate in turn, obtain the number of strings whose length is N, and then obtain the number of input strings whose length is N. Note that n needs to be-1 after calculation, ah, I didn't do this at first. There was a bug. I will add it later.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>typedef long long ll;using namespace std;const int maxn = 3000;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;}int main() {init();while (scanf("%d", &n) && n) {int len = 1;while (n > num[len]) {n -= num[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;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.