1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
using boost::lexical_cast;
using std::cout;
using std::endl;
using std::sort;
using std::string;
void Run( int target, const string& str, int sum, const int* first, const int* last )
{
for( ; first != last; ++first )
if( sum + *first == target )
cout << str << ( str.empty() ? "" : " + " ) << *first
<< " = " << target << endl;
else if( sum + *first < target )
Run( target, str + ( str.empty() ? "" : " + " ) +
lexical_cast<string>( *first ), sum + *first, first + 1, last );
}
int main() {
int nums[40] = {
103, 107, 109, 113, 117, 119, 127, 131, 133, 137,
1, 2, 3, 5, 7, 11, 13, 17, 19, 23,
67, 71, 73, 79, 83, 87, 89, 91, 97, 101,
29, 31, 37, 41, 43, 47, 51, 53, 59, 61
};
sort( &nums[0], &nums[40] );
Run( 50, string(), 0, &nums[0], &nums[40] );
}
|