Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Bad palindrom
//Title of this code #include <iostream> #include <string> using namespace std; bool isPalindrom(int num) { string str = to_string(num); int i = 0; int j = str.length() - 1; while (i < j) { if (str[i] != str[j]) return false; ++i; --j; } return true; } int nextPalindrom(int n) { if (isPalindrom(n)) return n; do { ++n; } while (!isPalindrom(n)); return n; } int nextPalindrom2(int num) { string str = to_string(num); int i = 0; int j = str.length() - 1; //cout << str << endl; while (i < j) { int a = str[i] - '0'; int b = str[j] - '0'; if (a != b) { if (a < b) { str[i] = (a + 1) + '0'; str[j] = str[i]; //cout << "anyad: " << str[i] << str[j] << endl; } else { //str[j] = (b - 1) + '0'; //str[i] = str[j]; str[j] = str[i]; } } //str[j] = str[i]; ++i; --j; } return atoi(str.c_str()); } int main() { //cout << 57 << " " << nextPalindrom(57) << " - " << nextPalindrom2(57) << endl; for (int i = 0; i < 9000; ++i) if (nextPalindrom(i) != nextPalindrom2(i)) cout << i << " " << nextPalindrom(i) << " - " << nextPalindrom2(i) << endl; //std::cout << nextPalindrom2(9865); }
run
|
edit
|
history
|
help
0
an awesome question of basic graph traversal (786A)
Click 5
weak_ptr and Circle_reference
fibonacci
Dar
3 and 7 in a row
TempSpecial
Operators
static property
Interview Prep