自用PTA题目记录0016
作者:互联网
自用PTA题目记录0016
以下题目序号并无实际意义
文章目录
7-17 构造回文数
题目作者: 胡伟平 单位: 广西科技大学 代码长度限制: 16 KB 时间限制: 400 ms 内存限制: 64 MB
回文数是一种很有趣的数,对于一个不是回文数的数,我们可以通过以下操作来将其变成回文数:将数反转,加到原数上,重复这个过程,直到得到回文数为止。
输入格式: 输入1个数位不超过1000的正整数。
输出格式: 对每一个输入,输出将其变成回文数的过程。每一行按以下个数输出:
A + B = C
A是原数,B是反转数,C是和,重复这个过程,直到C是回文数为止。在最后一行输出C is a palindromic number.。为了控制循环次数,我们规定,如果经过10次操作,还得不到回文数,那么停止,显示Not found in 10 iterations.
输入样例1:
1234
输出样例1:
1234 + 4321 = 5555
5555 is a palindromic number.
输入样例2:
1239102349120349
输出样例2:
1239102349120349 + 9430219432019321 = 10669321781139670
10669321781139670 + 07693118712396601 = 18362440493536271
18362440493536271 + 17263539404426381 = 35625979897962652
35625979897962652 + 25626979897952653 = 61252959795915305
61252959795915305 + 50351959795925216 = 111604919591840521
111604919591840521 + 125048195919406111 = 236653115511246632
236653115511246632 + 236642115511356632 = 473295231022603264
473295231022603264 + 462306220132592374 = 935601451155195638
935601451155195638 + 836591551154106539 = 1772193002309302177
1772193002309302177 + 7712039032003912771 = 9484232034313214948
Not found in 10 iterations.
代码
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string stringAdd(string, string);
int main(int argc, char const *argv[])
{
string str01;
string str02;
string str03;
int chI;
cin >> str01;
str02 = str01;
reverse(str02.begin(), str02.end());
if (!str01.compare(str02))
{
cout << str01 << " is a palindromic number." << endl;
return 0;
// good for you, question author?
}
for (int i = 0; i < 10; i++)
{
str02 = str01;
reverse(str02.begin(), str02.end());
str03 = stringAdd(str01, str02);
cout << str01 << " + " << str02 << " = " << str03 << endl;
str01 = str03;
reverse(str03.begin(), str03.end());
if (!str01.compare(str03))
{
cout << str01 << " is a palindromic number." << endl;
return 0;
}
}
cout << "Not found in 10 iterations." << endl;
return 0;
}
string stringAdd(string str01, string str02)
{
string result = "";
char ch;
int chI;
bool aFlag = false;
for (int i = 0; i < str01.length(); i++)
{
chI = str01[str01.length() - 1 - i] + str02[str02.length() - 1 - i] - 96;
if (aFlag)
{
chI++;
}
if (chI >= 10)
{
aFlag = true;
ch = chI + 38;
}
else
{
ch = chI + 48;
aFlag = false;
}
result = ch + result;
}
if (aFlag)
{
result = "1" + result;
}
return result;
}
总结
备注位置是这题的大坑,给我卡了很久,这个题目作者脑回路我对不上
PTA提交通过截图
标签:0016,string,chI,PTA,自用,result,str01,str02,回文 来源: https://blog.csdn.net/ELT_Core/article/details/110704591