1024x768. Palindromic Number (25)

Source: Internet
Author: User

A number that would be the same when it was written forwards or backwards is known as a palindromic number. For example, 1234321 is a palindromic number. All digit numbers is palindromic numbers.

Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result was added to the original number. If The result is a palindromic number, this is repeated until it gives a palindromic number. For example, if the we start from the, we can obtain a palindromic number in 2 steps:67 + = 143, and 143 + 341 = 484.

Given any positive an integer N, you is supposed to find it paired palindromic number and the number of steps taken to find It.

Input Specification:

Each input file contains the one test case. Each case consists of a positive numbers n and K, where N (<= 1010) is the initial numer and K (<=) are the max Imum number of steps. The numbers is separated by a space.

Output Specification:

For each test case, output of the numbers, one in each line. The first number is the paired palindromic number of N, and the second number are the number of steps taken to find the PAL Indromic number. If the palindromic number is not found after K steps, just output of the number obtained at the Kth step and K instead.

Sample Input 1:
67 3
Sample Output 1:
4842
Sample Input 2:
69 3
Sample Output 2:

13533

#include <iostream> #include <vector> #include <string.h>using namespace Std;bool ispalindromic (    Vector<int> a) {int i, J;    For (I=0, J=a.size ()-1; i<=j; i++, j--) {if (A[i]! = A[j]) return false; } return true;    void print (vector<int> input) {int i;    For (I=input.size ()-1; i>=0; i--) {cout<<input[i]; } Cout<<endl;}    int main () {vector<int> input;    Char s[11];    int k, I;    cin>>s>>k;    int len = strlen (s);    for (i=0; i<len; i++) {input.push_back (s[i]-' 0 ');        } if (ispalindromic (input)) {print (input);    cout<< "0" <<endl;        } else {int len_temp = Len;            for (I=1; i<=k; i++) {int end = Len_temp-1;            int start = 0;            int j = 0;            int t = 0;//carry indicator vector<int> vec_temp;               for (; start<len_temp && end>=0; start++, End--, J + +) { Vec_temp.push_back (t + Input[start] + input[end]);                    if (Vec_temp[j] >=) {vec_temp[j]-= 10;                t = 1;                } else {t = 0;            }} if (T > 0) {//High-vec_temp.push_back (1);            } len_temp = Vec_temp.size ();            Input.assign (Vec_temp.begin (), Vec_temp.end ());        if (ispalindromic (input)) break;        } print (input);        if (i>k) cout<<k<<endl;    else cout<<i<<endl; } return 0;}


1024x768. Palindromic Number (25)

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.