A1024 Palindromic Number

摘要
Title: A1024 Palindromic Number
Tag: 高精度
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

A1024 Palindromic Number

  • 题意

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are 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 is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.
    Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

  • 思路

    高精加

  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    /*
    * @Author: NEFU AB-IN
    * @Date: 2023-01-07 10:24:52
    * @FilePath: \GPLT\A1024\A1024.cpp
    * @LastEditTime: 2023-01-07 10:37:03
    */
    #include <bits/stdc++.h>
    using namespace std;
    #define N n + 100
    #define int long long
    #define SZ(X) ((int)(X).size())
    #define IOS \
    ios::sync_with_stdio(false); \
    cin.tie(nullptr); \
    cout.tie(nullptr)
    #define DEBUG(X) cout << #X << ": " << X << '\n'
    typedef pair<int, int> PII;

    // #undef N
    // const int N = 1e5 + 10;

    #undef int

    vector<int> add(vector<int> &a, vector<int> &b)
    {
    int t = 0;
    vector<int> c;
    for (int i = 0; i < SZ(a); ++i)
    {
    int s = a[i] + b[i] + t;
    c.push_back(s % 10);
    t = s / 10;
    }
    if (t)
    c.push_back(t);
    return c;
    }

    bool check(vector<int> a)
    {
    for (int i = 0; i < SZ(a); ++i)
    {
    if (a[i] != a[SZ(a) - 1 - i])
    return false;
    }
    return true;
    }

    signed main()
    {
    IOS;
    string s;
    int k;
    cin >> s >> k;

    vector<int> a;
    for (int i = SZ(s) - 1; i >= 0; --i)
    a.push_back(s[i] - '0');
    int cnt = 0;
    for (int i = 1; i <= k; ++i)
    {
    if (check(a))
    break;
    vector<int> b = a;
    reverse(b.begin(), b.end());
    vector<int> c = add(a, b);
    a = c;
    cnt++;
    }

    for (int i = SZ(a) - 1; i >= 0; --i)
    cout << a[i];
    cout << '\n' << cnt;
    return 0;
    }
使用搜索:谷歌必应百度