Skip to content

Instantly share code, notes, and snippets.

@hempnall
Created February 6, 2018 19:49
Show Gist options
  • Save hempnall/a1d995e4912579e54eb0bc656bd915a4 to your computer and use it in GitHub Desktop.
Save hempnall/a1d995e4912579e54eb0bc656bd915a4 to your computer and use it in GitHub Desktop.
Pick up to M from N numbers from a list
#include <iostream>
#include <vector>
using namespace std;
typedef std::vector<uint8_t> choices_t;
void printVec( choices_t& picked ) {
for (uint8_t num : picked) cout << (int) num << " ";
cout << "\n";
}
void pickInt( choices_t& picked , uint8_t choice )
{
picked.push_back( choice);
}
void popInt( choices_t& picked )
{
picked.pop_back();
}
void MfromN( uint8_t m, uint8_t n , uint8_t n_start , choices_t& picked)
{
if ( m ==0) {
printVec(picked);
return;
}
for (int i= n_start ; i < n ; ++i) {
pickInt( picked , i );
MfromN( m - 1, n , i + 1, picked );
popInt( picked );
}
}
void uptoMfromN( uint8_t m , uint8_t n )
{
if (m == 0) return;
std::vector<uint8_t> picked;
MfromN( m , n , 0 , picked);
uptoMfromN( m - 1 , n);
}
int main()
{
uint8_t res[1];
std::vector<uint8_t> picked;
uptoMfromN( 3,6 );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment