Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 7th, 2012  |  syntax: None  |  size: 0.58 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Todas las combinaciones de N elementos
  2. void Combinaciones( int N ) {
  3.         for( int n = 0; n < 1 << N; n++ ) {
  4.                 // Usar combinación.
  5.         }
  6. }
  7.  
  8. // Combinaciones de N elementos tomados de M en M
  9. void Combinaciones( int N, int M ) {
  10.         for( int n = 0; n < 1 << N; n++ ) {
  11.  
  12.                 // Tomada de http://graphics.stanford.edu/~seander/bithacks.html
  13.                 // Cuenta el número de bits a 1 (sólo para 32 bits)
  14.                 int v = n - ( ( n >> 1 ) & 0x55555555 );
  15.                 v = ( v & 0x33333333 ) + ( ( v >> 2 ) & 0x33333333 );
  16.                 v = ( ( v + ( v >> 4 ) & 0xF0F0F0F ) * 0x1010101 ) >> 24;
  17.  
  18.                 if( v == M ) {
  19.                         // Usar combinación.
  20.                 }
  21.         }
  22. }