PAT 1136

题目 : A Delayed Palindrome

分值 : 20
难度 : 简单题
思路 : string 模拟大树运算
坑点 : 暂未发现

具体代码如下

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
#include <iostream>
#include <algorithm>
using namespace std;
bool judge(string s)
{
string temp = s;
reverse(temp.begin() , temp.end()) ;
return s == temp ;
}
string deal(string s)
{
string temp = s ;
reverse(temp.begin(),temp.end()) ;
int Cy = 0 ;
string result ;
for(int i = s.size()-1 ; i>= 0 ; i--)
{
int c1 = s[i] - '0' ;
int c2 = temp[i] - '0' ;
int end = c1 + c2 +Cy ;
if(end >= 10)
{
Cy = 1 ;
end -=10 ;
}
else
Cy = 0 ;
char ch = '0'+ end ;
result += ch ;
}
if(Cy)
result += '1' ;
reverse(result.begin() , result.end()) ;
return result ;
}
int main() {
string s ;
cin >> s;
int count = 0 ;
while(count <10)
{
if(judge(s))
{
cout << s <<" is a palindromic number."<<endl ;
break ;
}
else
{
string s2 = s ;
reverse(s2.begin() ,s2.end()) ;
cout << s <<" + "<<s2 <<" = "<< deal(s) <<endl ;
s = deal(s) ;
}
count ++ ;
}
if(count >= 10 )
cout <<"Not found in 10 iterations."<<endl;
}