#include<bits/stdc++.h>
using namespace std;
bool isPalindeomic(string S) {
// 判断回文数
for(int i = 0;i < S.size()/2;++i)
if(S[i] != S[S.size() - 1 - i])
return false;
return true;
}
string add(string a, string b) {
// 大整数加法
string rst;
int carry = 0;
for(int i = a.size() - 1;i >= 0;--i) {
// 从数的低位往高位遍历
int t = a[i] + b[i]- 2*'0' + carry;
carry = t / 10;
rst.push_back(t % 10 + '0');
}
if(carry > 0) rst.push_back(carry + '0');
reverse(rst.begin(), rst.end()); // 加完的数是反着存储的,所以要reverse
return rst;
}
int main() {
int maxStep;
string strNum;
cin >> strNum >> maxStep;
int step;
for(step = 0;step < maxStep;++step) {
if(isPalindeomic(strNum)) break;
string num = strNum;
reverse(begin(strNum), end(strNum));
string rev_num = strNum;
strNum = add(num, rev_num);
}
cout << strNum << endl;
step == maxStep ? cout<<maxStep : cout<<step;
return 0;
}
【PAT甲级 大整数加法】1024 Palindromic Number (25 分)
猜你喜欢
转载自blog.csdn.net/MYMarcoreus/article/details/114480063
今日推荐
周排行