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>


std::pair<std::string, std::string> splitString( const std::string & s ,
long string2long( const std::string & s ) {
long result ;
int pos ) {
return std::make_pair( s.substr( 0 , pos ) , s.substr( 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::pair<std::string, std::string> stringparts =
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 ( stringparts.second.find_first_not_of( "0" ) == std::string::npos ) {
if ( secondpart.find_first_not_of( "0" ) == std::string::npos ) {
return false ;
return false ;
}
}
if ( atol( stringparts.first.c_str( ) )
if ( string2long( firstpart ) + string2long( secondpart ) == number ) {
+ atol( stringparts.second.c_str( )) == number ) {
return true ;
return true ;
}
}