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 34 35 36 37 38 39 40 41 42 43 44 45
|
#include <iostream>
#include <vector>
#include <set>
using score_list = std::vector<int> ;
std::set<int> possible_total_scores( std::vector<score_list>::const_iterator begin,
std::vector<score_list>::const_iterator end )
{
std::size_t nlists = end - begin ;
if( nlists == 0 ) return {} ; // no score lists; return empty set
else if( nlists == 1 ) return { begin->begin(), begin->end() } ; // only one score list, return the unique values in it
else // two ore more score lists
{
// get possible total scores for the tail (every list except the first)
const std::set<int> tail = possible_total_scores( begin+1, end ) ;
std::set<int> result ;
// add every score in the first list to each possible_total_score for the tail
for( int score : *begin ) for( int total : tail ) result.insert(score+total) ;
return result ;
}
}
int main()
{
std::vector<score_list> score_lists =
{
{ 2, 4, 6 },
{ 2, 8 },
{ 4, 6 },
{ 4, 6, 8, 8 },
{ 0, 2, 4, 6, 8, 8 },
{ 4, 4, 4, 8, 8, 8, 8 },
{ 0, 0, 0, 2, 2, 2, 4, 4, 4, 6, 6, 6, 8, 8, 8 },
{ 7, 7, 7, 9, 9, 9, 9 }
};
const auto set = possible_total_scores( score_lists.begin(), score_lists.end() ) ;
int n = 0 ;
for( int total_score : set ) std::cout << ++n << ". " << total_score << '\n' ;
}
|