给定一个 k+1 位的正整数 N,写成 ak⋯a1a0 的形式,其中对所有 i 有 0≤ai<10 且 ak>0。N 被称为一个回文数,当且仅当对所有 i 有 ai=ak−i。零也被定义为一个回文数。
非回文数也可以通过一系列操作变出回文数。首先将该数字逆转,再将逆转数与该数相加,如果和还不是一个回文数,就重复这个逆转再相加的操作,直到一个回文数出现。如果一个非回文数可以变出回文数,就称这个数为延迟的回文数。(定义翻译自 https://en.wikipedia.org/wiki/Palindromic_number )
给定任意一个正整数,本题要求你找到其变出的那个回文数。
输入格式:
输入在一行中给出一个不超过1000位的正整数。
输出格式:
对给定的整数,一行一行输出其变出回文数的过程。每行格式如下
A + B = C
其中 A 是原始的数字,B 是 A 的逆转数,C 是它们的和。A 从输入的整数开始。重复操作直到 C 在 10 步以内变成回文数,这时在一行中输出 C is a palindromic number.;或者如果 10 步都没能得到回文数,最后就在一行中输出 Not found in 10 iterations.。
输入样例 1:
97152
输出样例 1:
97152 + 25179 = 122331
122331 + 133221 = 255552
255552 is a palindromic number.
输入样例 2:
196
输出样例 2:
196 + 691 = 887
887 + 788 = 1675
1675 + 5761 = 7436
7436 + 6347 = 13783
13783 + 38731 = 52514
52514 + 41525 = 94039
94039 + 93049 = 187088
187088 + 880781 = 1067869
1067869 + 9687601 = 10755470
10755470 + 07455701 = 18211171
Not found in 10 iterations.
思路:做这个题的时候,没多想,直接上手用string转int做了,结果,最后一个点迟迟过不了,再读题发现是1000位的大数,用int就太小了,只能再换成字符串的加法。
测试点0、1、5只要样例过即可通过
测试点2、3、4为输入值就是回文数,单独考虑一下即可
测试点6即为大数测试点
测试点6未过的代码:
注意这是未过的,全过的在下面,想的是记录一下做题过程
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool hw(string s);
int main()
{
string s;
int a, b, c;
string sa, sb, sc;
cin >> s;
if (hw(s))
{
cout << s << " is a palindromic number.";
return 0;
}
int n = 10;
while (n--)
{
a = atoi(s.c_str());
s.assign(s.rbegin(), s.rend());//也可以用reverse函数
b = atoi(s.c_str());
c = a + b;
sa = to_string(a);
sb = to_string(b);
sc = to_string(c);
if (hw(sc))
{
cout << sa << " + " << sb << " = " << sc << endl << sc << " is a palindromic number.";
return 0;
}
else
cout << sa << " + " << sb << " = " << sc << endl;
s = sc;
}
cout << "Not found in 10 iterations.";
return 0;
}
bool hw(string s)
{
int n = s.size();
for (int i = 0; i < n / 2; i++)
if (s[i] != s[n - i - 1])
return false;
return true;
}
正确的代码(大数加和部分,参考于(大数)大整数加法 C++ 示例代码)
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool hw(string s);
string sadd(string a, string b);
int main()
{
string s;
string sa, sb, sc;
cin >> s;
if (hw(s))
{
cout << s << " is a palindromic number.";
return 0;
}
int n = 10;
while (n--)
{
sa = s;
s.assign(s.rbegin(), s.rend());//也可以用reverse函数
sb = s;
sc = sadd(sa, sb);
if (hw(sc))
{
cout << sa << " + " << sb << " = " << sc << endl << sc << " is a palindromic number.";
return 0;
}
else
cout << sa << " + " << sb << " = " << sc << endl;
s = sc;
}
cout << "Not found in 10 iterations.";
return 0;
}
bool hw(string s)
{
int n = s.size();
for (int i = 0; i < n / 2; i++)
if (s[i] != s[n - i - 1])
return false;
return true;
}
string sadd(string a, string b)
{
string s = "";
int len1 = a.size();
int len2 = b.size();
if (len1 > len2)
b.insert(b.begin(), len1 - len2, '0');
else if (len1 < len2)
a.insert(a.begin(), len2 - len1, '0');
int jw = 0;
int len = a.size();
for (int i = len - 1; i >= 0; i--)
{
int sum = (a[i] - '0') + (b[i] - '0') + jw;
jw = 0;
s.insert(s.begin(), (sum % 10) + '0');
if (sum / 10)
jw++;
}
if (jw)
s.insert(s.begin(), jw + '0');
return s;
}
通过截图
如果有什么问题,欢迎在评论区留言~