我的个人网站 Cheese的个人主页http://www.cheese.ren/
博客来源 PAT 乙级 1079 延迟的回文数-Cheese的个人博客http://blog.cheese.ren/99
欢迎交换友链 :-)
#include <bits/stdc++.h>
using namespace std;
bool is_palindromic(string s) {
for (int i=0; i<s.length()/2; i++) {
if (s[i] != s[s.length()-i-1]) {
return false;
}
}
return true;
}
int main() {
string s; // 输入的数字
bool flag=false; // false:不是回文;true:是回文
cin >> s;
if (is_palindromic(s)) {
flag = true;
}
for (int i=0; i<10 && !flag; i++) {
string a, b;
int array=0;
a = s;
b = s;
reverse(b.begin(), b.end());
for (int i=s.length()-1; i>=0; i--) {
s[i] = (a[i] + b[i] + array - '0' - '0')%10 + '0';
array = (a[i] + b[i] + array - '0' - '0')/10;
}
if (array != 0) {
s = char(array + '0') + s;
}
cout << a << " + " << b << " = " << s << endl;
if (is_palindromic(s)) {
flag = true;
}
}
if (flag) {
cout << s << " is a palindromic number." << endl;
}
else {
cout << "Not found in 10 iterations." << endl;
}
return 0;
}