-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1079.cpp
More file actions
59 lines (51 loc) · 1.04 KB
/
1079.cpp
File metadata and controls
59 lines (51 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
void cal(string &s)//大数加法
{
string tem, res;
int carry=0;//进位
tem = s;
reverse(tem.begin(), tem.end());//reverse函数可以进行反转
for (int i = s.length() - 1; i >= 0; i--)
{
if (s[i] - '0' + tem[i] - '0' + carry < 10)//两者相加小于10
{
res += s[i] - '0' + tem[i] - '0' + carry + '0';
carry = 0;//进位为0
}
else//两者相加大于10
{
res += s[i] - '0' + tem[i] - '0' + carry-10 + '0';
carry = 1;//进位为1
}
}
if (carry == 1)//最高位的进位
res += '1';
reverse(res.begin(), res.end());//反转后的才是真正的和
cout << s << " + " << tem << " = " << res << endl;
s = res;
}
bool IsHuiwen(string s)//判断是否回文
{
string t = s;
reverse(t.begin(), t.end());
return s == t;
}
int main()
{
string s;
cin >> s;
int count = 0;
while (!IsHuiwen(s) && count < 10)//当已经转成回文或步骤大于10步时退出
{
cal(s);
count++;
}
if (IsHuiwen(s))
cout << s << " is a palindromic number.\n";
else
cout << "Not found in 10 iterations.\n";
return 0;
}