Skip to content

Commit

Permalink
🐛 bug: fix a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchuo committed Sep 23, 2018
1 parent c85f368 commit 7e58636
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 56 deletions.
54 changes: 26 additions & 28 deletions AdvancedLevel_C++/1136. A Delayed Palindrome (20).cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
#include <iostream>
#include <algorithm>
using namespace std;
string add(string a) {
string b = a, ans;
reverse(b.begin(), b.end());
int len = a.length(), carry = 0;
for (int i = 0; i < len; i++) {
int num = (a[i] - '0' + b[i] - '0') + carry;
carry = 0;
if (num >= 10) {
carry = 1;
num = num - 10;
}
ans += char(num + '0');
string rev(string s) {
reverse(s.begin(), s.end());
return s;
}
string add(string s1, string s2) {
string s = s1;
int carry = 0;
for (int i = s1.size() - 1; i >= 0; i--) {
s[i] = (s1[i] - '0' + s2[i] - '0' + carry) % 10 + '0';
carry = (s1[i] - '0' + s2[i] - '0' + carry) / 10;
}
if(carry == 1) ans += '1';
reverse(ans.begin(), ans.end());
return ans;
if (carry > 0) s = "1" + s;
return s;
}
int main() {
string s;
string s, sum;
int n = 10;
cin >> s;
int cnt = 0;
while (cnt < 10) {
string t = s;
reverse(t.begin(), t.end());
if (t == s) {
cout << s << " is a palindromic number.";
break;
} else {
cout << s << " + " << t << " = " << add(s) << endl;
s = add(s);
cnt++;
if (s == rev(s)) {
cout << s << " is a palindromic number.\n";
return 0;
}
while (n--) {
sum = add(s, rev(s));
cout << s << " + " << rev(s) << " = " << sum << endl;
if (sum == rev(sum)) {
cout << sum << " is a palindromic number.\n";
return 0;
}
s = sum;
}
if (cnt == 10) cout << "Not found in 10 iterations.";
cout << "Not found in 10 iterations.\n";
return 0;
}
54 changes: 26 additions & 28 deletions BasicLevel_C++/1079. 延迟的回文数 (20).cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
#include <iostream>
#include <algorithm>
using namespace std;
string add(string a) {
string b = a, ans;
reverse(b.begin(), b.end());
int len = a.length(), carry = 0;
for (int i = 0; i < len; i++) {
int num = (a[i] - '0' + b[i] - '0') + carry;
carry = 0;
if (num >= 10) {
carry = 1;
num = num - 10;
}
ans += char(num + '0');
string rev(string s) {
reverse(s.begin(), s.end());
return s;
}
string add(string s1, string s2) {
string s = s1;
int carry = 0;
for (int i = s1.size() - 1; i >= 0; i--) {
s[i] = (s1[i] - '0' + s2[i] - '0' + carry) % 10 + '0';
carry = (s1[i] - '0' + s2[i] - '0' + carry) / 10;
}
if(carry == 1) ans += '1';
reverse(ans.begin(), ans.end());
return ans;
if (carry > 0) s = "1" + s;
return s;
}
int main() {
string s;
string s, sum;
int n = 10;
cin >> s;
int cnt = 0;
while (cnt < 10) {
string t = s;
reverse(t.begin(), t.end());
if (t == s) {
cout << s << " is a palindromic number.";
break;
} else {
cout << s << " + " << t << " = " << add(s) << endl;
s = add(s);
cnt++;
if (s == rev(s)) {
cout << s << " is a palindromic number.\n";
return 0;
}
while (n--) {
sum = add(s, rev(s));
cout << s << " + " << rev(s) << " = " << sum << endl;
if (sum == rev(sum)) {
cout << sum << " is a palindromic number.\n";
return 0;
}
s = sum;
}
if (cnt == 10) cout << "Not found in 10 iterations.";
cout << "Not found in 10 iterations.\n";
return 0;
}

0 comments on commit 7e58636

Please sign in to comment.