题目描述
知识点: 大数加法,模拟
思路: 一个简单的大数加法 + 判断回文数进行模拟。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> add(vector<int> a,vector<int> b){
vector<int> c;
int t = 0;
for(int i = a.size()-1;i >= 0;i--){
t += a[i] + b[i];
c.push_back(t % 10);
t /= 10;
}
if(t) c.push_back(t);
reverse(c.begin(),c.end());
return c;
}
bool check(vector<int> a){
for(int i = 0,j = a.size()-1;i < j;i++,j--){
if(a[i] != a[j]) return false;
}
return true;
}
void print(vector<int> a){
for(auto i : a) cout<<i;
}
int main(){
string s;
cin>>s;
vector<int> a;
for(auto i : s)
a.push_back(i-'0');
vector<int> c = a;
int ite = 0;
while(!check(c) && ite < 10){
vector<int> b = c;
a = c;
reverse(b.begin(),b.end());
c = add(a,b);
ite++;
print(a);
cout<<" + ";
print(b);
cout<<" = ";
print(c);
cout<<endl;
}
if(ite >= 10) cout<<"Not found in 10 iterations.";
else {
print(c);
cout<<" is a palindromic number.";
}
return 0;
}