The code is working, but unreadable. How to simplify it.

#include <iostream> using namespace std; void main() { int num; cin >> num; int roz = 1; int res = num; while( res / 10 ) { ++roz; res = res / 10; } int left = 0; int right = 0; bool answer = false; for( int i = roz; i >= 1; i-- ) { for( int j = -roz; j < 0; j++ ) { left = int( num / pow( 10, i - 1 ) ) % 10; right = int( num / pow( 10, roz + j ) ) % 10; cout << left << " || " << right << endl; if( left == right ) { i--; answer = true; continue; } else { answer = false; } i = 0; break; } } answer ? ( cout << "yes" ) : ( cout << "no\n" ); } 
  • one
    The code is not only unreadable (corrected formatting), but also not compiled: 1) void main() 2) forgotten either <math.h> or <cmath> . PS Do you have to work with the number? Since it is entered, it is entered as a string, and the detection of a palindrome in a string is much easier. - PinkTux
  • However, the algorithm also fits in a couple of lines, without any pow() and nested loops, :) - PinkTux
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

If the number comes from stdin , then pretend to be lazy and treat it as a string:

 int is_palindrome( const char *s, size_t length ) { size_t i = 0, j = length ? length : strlen( s ); // Требуемый цикл: while( i <= j ) { if( s[i++] != s[--j] ) { return 0; } } return 1; } int is_palindrome( const string &s ) { return is_palindrome( s.c_str(), s.size() ); } 

But if you got a teacher without a sense of humor, or the number really comes as a number ... Just turn the original number backwards and look at the result. It turned out the same number - hooray, palindrome:

 int is_palindrome( unsigned long n ) { unsigned long m = n; unsigned long flip = 0; // Требуемый цикл: while( m > 0 ) { flip *= 10; flip += m % 10; m /= 10; } return flip == n; } 

    A slightly optimized variant of the neighboring answer (almost twice as short as the steps of the cycle)

     int is_palindrome(unsigned long n1){ unsigned long n2 = 0; if (!(n1 % 10)) return 0; while (n2 < n1) { n2 *= 10; n2 += n1 % 10; n1 /= 10; } // Идея заключается в том, что для палиндромов с четным количеством // цифр работает условие n2 == n1. // При нечетном количестве цифр у числа n2 будет на одну цифру больше // (поскольку цикл завершается при n2 >= n1) причем эта // цифра - младший разряд. Поскольку она на результат не влияет, мы // избавляемся от нее путем целочисленного деления на 10 return n2 == n1 || n2 / 10 == n1; } 

    See examples

    • It seems to not work on 11000 - VladD
    • @VladD I'll be thinking :) - tutankhamun
    • As well as 10, 100, 1000 ... - PinkTux
    • @tutankhamun: In theory, if the number ends in 0 (but not equal to 0), then this is guaranteed not by a palindrome. - VladD
    • @VladD Perhaps it all depends on the conditions of the problem. Probably add a condition in response, but given the impossibility of writing a fixed number of zeros to the left in a numeric variable, I would consider 11000 a palindrome - tutankhamun