File tree Expand file tree Collapse file tree 2 files changed +52
-56
lines changed Expand file tree Collapse file tree 2 files changed +52
-56
lines changed Original file line number Diff line number Diff line change 1
1
#include < iostream>
2
2
#include < algorithm>
3
3
using namespace std ;
4
- string add (string a) {
5
- string b = a, ans;
6
- reverse (b.begin (), b.end ());
7
- int len = a.length (), carry = 0 ;
8
- for (int i = 0 ; i < len; i++) {
9
- int num = (a[i] - ' 0' + b[i] - ' 0' ) + carry;
10
- carry = 0 ;
11
- if (num >= 10 ) {
12
- carry = 1 ;
13
- num = num - 10 ;
14
- }
15
- ans += char (num + ' 0' );
4
+ string rev (string s) {
5
+ reverse (s.begin (), s.end ());
6
+ return s;
7
+ }
8
+ string add (string s1, string s2) {
9
+ string s = s1;
10
+ int carry = 0 ;
11
+ for (int i = s1.size () - 1 ; i >= 0 ; i--) {
12
+ s[i] = (s1[i] - ' 0' + s2[i] - ' 0' + carry) % 10 + ' 0' ;
13
+ carry = (s1[i] - ' 0' + s2[i] - ' 0' + carry) / 10 ;
16
14
}
17
- if (carry == 1 ) ans += ' 1' ;
18
- reverse (ans.begin (), ans.end ());
19
- return ans;
15
+ if (carry > 0 ) s = " 1" + s;
16
+ return s;
20
17
}
21
18
int main () {
22
- string s;
19
+ string s, sum;
20
+ int n = 10 ;
23
21
cin >> s;
24
- int cnt = 0 ;
25
- while (cnt < 10 ) {
26
- string t = s;
27
- reverse (t.begin (), t.end ());
28
- if (t == s) {
29
- cout << s << " is a palindromic number." ;
30
- break ;
31
- } else {
32
- cout << s << " + " << t << " = " << add (s) << endl;
33
- s = add (s);
34
- cnt++;
22
+ if (s == rev (s)) {
23
+ cout << s << " is a palindromic number.\n " ;
24
+ return 0 ;
25
+ }
26
+ while (n--) {
27
+ sum = add (s, rev (s));
28
+ cout << s << " + " << rev (s) << " = " << sum << endl;
29
+ if (sum == rev (sum)) {
30
+ cout << sum << " is a palindromic number.\n " ;
31
+ return 0 ;
35
32
}
33
+ s = sum;
36
34
}
37
- if (cnt == 10 ) cout << " Not found in 10 iterations." ;
35
+ cout << " Not found in 10 iterations.\n " ;
38
36
return 0 ;
39
37
}
Original file line number Diff line number Diff line change 1
1
#include < iostream>
2
2
#include < algorithm>
3
3
using namespace std ;
4
- string add (string a) {
5
- string b = a, ans;
6
- reverse (b.begin (), b.end ());
7
- int len = a.length (), carry = 0 ;
8
- for (int i = 0 ; i < len; i++) {
9
- int num = (a[i] - ' 0' + b[i] - ' 0' ) + carry;
10
- carry = 0 ;
11
- if (num >= 10 ) {
12
- carry = 1 ;
13
- num = num - 10 ;
14
- }
15
- ans += char (num + ' 0' );
4
+ string rev (string s) {
5
+ reverse (s.begin (), s.end ());
6
+ return s;
7
+ }
8
+ string add (string s1, string s2) {
9
+ string s = s1;
10
+ int carry = 0 ;
11
+ for (int i = s1.size () - 1 ; i >= 0 ; i--) {
12
+ s[i] = (s1[i] - ' 0' + s2[i] - ' 0' + carry) % 10 + ' 0' ;
13
+ carry = (s1[i] - ' 0' + s2[i] - ' 0' + carry) / 10 ;
16
14
}
17
- if (carry == 1 ) ans += ' 1' ;
18
- reverse (ans.begin (), ans.end ());
19
- return ans;
15
+ if (carry > 0 ) s = " 1" + s;
16
+ return s;
20
17
}
21
18
int main () {
22
- string s;
19
+ string s, sum;
20
+ int n = 10 ;
23
21
cin >> s;
24
- int cnt = 0 ;
25
- while (cnt < 10 ) {
26
- string t = s;
27
- reverse (t.begin (), t.end ());
28
- if (t == s) {
29
- cout << s << " is a palindromic number." ;
30
- break ;
31
- } else {
32
- cout << s << " + " << t << " = " << add (s) << endl;
33
- s = add (s);
34
- cnt++;
22
+ if (s == rev (s)) {
23
+ cout << s << " is a palindromic number.\n " ;
24
+ return 0 ;
25
+ }
26
+ while (n--) {
27
+ sum = add (s, rev (s));
28
+ cout << s << " + " << rev (s) << " = " << sum << endl;
29
+ if (sum == rev (sum)) {
30
+ cout << sum << " is a palindromic number.\n " ;
31
+ return 0 ;
35
32
}
33
+ s = sum;
36
34
}
37
- if (cnt == 10 ) cout << " Not found in 10 iterations." ;
35
+ cout << " Not found in 10 iterations.\n " ;
38
36
return 0 ;
39
37
}
You can’t perform that action at this time.
0 commit comments