
Untitled
By: a guest on
Sep 7th, 2012 | syntax:
None | size: 0.58 KB | hits: 7 | expires: Never
// Todas las combinaciones de N elementos
void Combinaciones( int N ) {
for( int n = 0; n < 1 << N; n++ ) {
// Usar combinación.
}
}
// Combinaciones de N elementos tomados de M en M
void Combinaciones( int N, int M ) {
for( int n = 0; n < 1 << N; n++ ) {
// Tomada de http://graphics.stanford.edu/~seander/bithacks.html
// Cuenta el número de bits a 1 (sólo para 32 bits)
int v = n - ( ( n >> 1 ) & 0x55555555 );
v = ( v & 0x33333333 ) + ( ( v >> 2 ) & 0x33333333 );
v = ( ( v + ( v >> 4 ) & 0xF0F0F0F ) * 0x1010101 ) >> 24;
if( v == M ) {
// Usar combinación.
}
}
}