问题描述:给定一个数N和次数b,判断N经过多少次变换可以变为一个Palindromic Number。
解题思路:一个函数判断是否是Palindromic Number,一个做加法运算,循环判断即可。
AC代码:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool judge(string &s)
{
int n = s.length(), j;
j = n / 2;
for (int i = 0; i < j; ++i) {
if (s[i] != s[n - i - 1])return false;
}
return true;
}
void add(string &s)
{
string x = s, y = "";
int n = s.length(), a = 0;
reverse(x.begin(), x.end());
for (int i = 0; i < n; ++i) {
int j = x[i] + s[i] - 2 * '0' + a;
y += j % 10 + '0';
a = j / 10;
}
if (a)y += '1';
reverse(y.begin(), y.end());
s = y;
}
int main()
{
freopen("test.txt", "r", stdin);
string str, s = "";
int n, m = 0;
cin >> str >> n;
while (!judge(str)) {
if (m >= n)break;
add(str);
++m;
}
cout << str << "\n" << m;
return 0;
}