PAT甲级 1024 Palindromic Number(25) (高精度+回文串)

题目

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.

输入

Each input file contains one test case. Each case consists of two positive numbers N and K, where N (≤10^{10}) is the initial numer and K (≤100) is the maximum number of steps. The numbers are separated by a space.

输出

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

样例输入 

样例输入1:

67 3

样例输入2:

69 3

样例输出 

样例输出1:

484
2

样例输出2:

1353
3

题意理解

给你一个数,给你一个最大步数,如果把这个数翻转加上原来的那个数是回文的话,那就可以跳出循环了,我们在最小步数中找到了回文数,如果没找到那就继续加

让你判断在这些步数中能否加出一个回文数,如果可以那么我们就返回那个回文数和步数,如果在最大步数中还是不能变成回文数,那么我们返回那个数加上最大步数。

题目的坑点主要是如果一开始就是个回文数的话,那么直接0步就可以结束掉然后直接输出开始的数字

然后这题的数字很大,我们每次add加法都用字符串去存结果

简单判断一个字符串是不是回文串,只要我们把它翻转然后还等于原来那个字符串,那么这个就是个回文串。

然后这题主要是高精度加法

我们从后往前将字符串每一位记录到vector里面

看位数哪个大,位数更大的那个就是更大的数,我们保持A就是最大数,所以如果B的位数多于A,我们交换A和B

t是每一位加法的进位

t加上A位数上面和B位数(如果B在这个位数上面有数字的话)上面的数字

t%10就是在这一位A+B的数字我们应该存放的数位是多少

t/=10就是我们存好这一位以后我们应该留下的进位

最后特判一下如果t还有的话,加到新的一位,也就是进位到新的位数

由于我们是从后往前存的A和B

那么我们将处理好的C翻转一下,就是我们想要的大数和

最后把这个C放回到字符串

那么我们这个大数加法就处理好了

代码 

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long LL;
string n;
int k;
bool check(string ss){
     string en=ss;
     reverse(en.begin(),en.end());
     if(ss==en)return true;
     else return false;
}
string add(string  a,string b){
    vector<int>A,B;
    for (int i=a.size()-1;i>=0;i--)A.push_back(a[i]-'0');
    for (int i=b.size()-1;i>=0;i--)B.push_back(b[i]-'0');
    if (A.size()<B.size())swap(A,B);
    vector<int> C;
    int t=0;
    for (int i=0;i<A.size();i++){
        t+=A[i];
        if(i<B.size())t+= B[i];
        C.push_back(t%10);
        t/=10;
    }
    if(t)C.push_back(t);
    string c="";
    reverse(C.begin(),C.end());
    for(int i=0;i<C.size();i++){
        c+=C[i]+'0';
    }
    return c;
}
int main(){
    cin>>n>>k;
    if(check(n)){
        cout<<n<<endl;
        cout<<"0"<<endl;
        return 0;
    }
    for(int i=1;i<=k;i++){
        string ss=n;
        reverse(ss.begin(),ss.end());
        n=add(ss,n);
        if(check(n)){
            cout<<n<<endl;
            cout<<i<<endl;
            return 0;
        }
    }
    cout<<n<<endl;
    cout<<k<<endl;
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值