Kaprekar numbers: Difference between revisions
Content deleted Content added
m whitespace |
|||
Line 64: | Line 64: | ||
#include <iostream> |
#include <iostream> |
||
#include <sstream> |
#include <sstream> |
||
#include <cstdlib> |
|||
#include <algorithm> |
#include <algorithm> |
||
#include <iterator> |
#include <iterator> |
||
#include <utility> |
#include <utility> |
||
long string2long( const std::string & s ) { |
|||
long result ; |
|||
int pos ) { |
|||
std::istringstream( s ) >> result ; |
|||
return result ; |
|||
} |
} |
||
Line 80: | Line 80: | ||
std::string numberstring = numberbuf.str( ) ; |
std::string numberstring = numberbuf.str( ) ; |
||
for ( int i = 0 ; i < numberstring.length( ) ; i++ ) { |
for ( int i = 0 ; i < numberstring.length( ) ; i++ ) { |
||
std::string firstpart = numberstring.substr( 0 , i ) , |
|||
secondpart = numberstring.substr( i ) ; |
|||
splitString( numberstring , i ) ; |
|||
//we do not accept figures ending in a sequence of zeroes |
//we do not accept figures ending in a sequence of zeroes |
||
if ( |
if ( secondpart.find_first_not_of( "0" ) == std::string::npos ) { |
||
return false ; |
return false ; |
||
} |
} |
||
if ( |
if ( string2long( firstpart ) + string2long( secondpart ) == number ) { |
||
+ atol( stringparts.second.c_str( )) == number ) { |
|||
return true ; |
return true ; |
||
} |
} |