#include <iostream> #include <vector> #include <algorithm> using namespace std; bool ili_eto_palindrom_ili_net( int chislo ) { bool ok = true; vector < char > agaga; for ( ' '; chislo; chislo /= 10 ) agaga.push_back( chislo % 10 ); for ( unsigned i = 0, j = agaga.size() - 1; i < j; ok = ok && agaga[ i++ ] == agaga[ j-- ] ); return ok; } int main() { int r = -1; for ( int x = 999; x; --x ) for ( int y = x; y; --y ) if ( ili_eto_palindrom_ili_net( x * y ) ) r = max ( r, x * y ); cout << r << endl; cin.get(); return 0; } 

The program multiplies two three-digit numbers from 100 to 999 and looks, this is a palindrome or not. And then from all palindromes finds the maximum. But when I run, the console is empty, what's wrong?

  • one
    Perhaps the problem was that in studio max is a macro that is poorly defined. - andrybak
  • 2
    This, by the way, is one of the reasons that it is bad to write using namespace std; . If we wrote std::max , then everything would be OK. - andrybak
  • Now I understand why the std :: method is written everywhere, and they don’t use using namespace std; - dajver

2 answers 2

Your code is very bad to read. Better explicit than not explicit. + Difficulty in debugging The second loop in the function loops. I suggest replacing the function with this one:

 bool ili_eto_palindrom_ili_net( int chislo ) { vector <int> agaga; for ( ; chislo; chislo /= 10 ) agaga.push_back( chislo % 10 ); for (int i = 0, j = agaga.size() - 1; i < agaga.size() / 2; i++) if (agaga[i] != agaga[j - i]) return false; return true; } 
  • it is good, but still the console does not produce anything ... - dajver
  • 2
    What are you compiling? I have everything in ubunta going and working fine. - skegg pm
  • VS 2010, compiles all the rules but a dark screen. Thanks for the help! The issue is resolved. - dajver

@enum responded generally correct. But I would still write a little differently. For example, so

 bool ili_eto_palindrom_ili_net( int chislo ) { vector < int > agaga; for ( ; chislo; chislo /= 10 ) agaga.push_back( chislo % 10 ); for ( int i = 0, j = agaga.size() - 1; i < j; i++, j-- ) if (agaga[i] != agaga[j] ) return false; return true; } int main() { int r = -1, t; for ( int x = 999; x >= 100; --x ) { for ( int y = x; y >= 100; --y ) { t = x*y; if ( ili_eto_palindrom_ili_net( t ) ) r = max ( r, t ); } } cout << "Max palindrome: " << r << endl; cin.get(); return 0; }