题意:
找一个回文数串
思路:
string模拟数串加法
注意,可能输入的就是合法的,那样就直接输出
#include<bits/stdc++.h>
#include<cstring>
#define FI first
#define SE second
using namespace std;
typedef long long ll;
typedef pair<string, string> P;
const int maxn = 1e4 + 7;
string s, t;
string add() {
int i = s.size() - 1, j = t.size() - 1;
string ans;
int num, c = 0;
for( ; i >= 0 || j >= 0; --i, --j) {
num = 0;
if(i >= 0) num += (s[i] - '0');
if(j >= 0) num += (t[j] - '0');
num += c; c = 0;
if(num >= 10) c = 1;
num %= 10;
ans.push_back((char)(num+'0'));
}
if(c) ans.push_back('1');
reverse(ans.begin(), ans.end());
return ans;
}
bool ok() {
for(int i = 0, j = s.size()-1; i <= j; ++i, --j) {
if(s[i] != s[j]) return false;
}
return true;
}
bool solve() {
t = s; reverse(t.begin(), t.end());
string x = add();
cout << s << " + " << t << " = " << x << endl;
s = x;
if(!ok()) return false;
cout << s << " is a palindromic number." << endl;
return true;
}
int main() {
cin >> s;
if(ok()) {
cout << s << " is a palindromic number." << endl;
return 0;
}
for(int i = 1; i <= 10; ++i) {
if(solve()) return 0;
}
puts("Not found in 10 iterations.");
return 0;
}