Combinations - Rosetta Code import java.util.Collections; import java.util.LinkedList; public class...

Please Visit: http://ift.tt/1ajReyV



Combinations - Rosetta Code

import java.util.Collections;

import java.util.LinkedList;



public class Comb{



public static void main( (Link->http://ift.tt/1qzhfEY) [] args){

(Link->http://ift.tt/1qzhi3E) .out.println(comb(3,5));

}



public static (Link->http://ift.tt/1qzhfEY) bitprint(int u){

(Link->http://ift.tt/1qzhfEY) s= "";

for(int n= 0;u > 0;++n, u>>= 1)

if((u & 1) > 0) s+= n + " ";

return s;

}



public static int bitcount(int u){

int n;

for(n= 0;u > 0;++n, u&= (u - 1));//Turn the last set bit to a 0

return n;

}



public static (Link->http://ift.tt/1qzhfEY) > comb(int c, int n){

(Link->http://ift.tt/1qzhfEY) >();

for(int u= 0;u < 1 << n;u++)

if(bitcount(u) == c) s.push(bitprint(u));

(Link->http://ift.tt/1qzhi3G) .sort(s);

return s;

}

}

http://ift.tt/1qzhi3J

http://ift.tt/1mzBJgg



Combinations - Rosetta Code







from Public RSS-Feed of Jeffery yuan. Created with the PIXELMECHANICS 'GPlusRSS-Webtool' at http://gplusrss.com http://ift.tt/1mzBJgk

via LifeLong Community

No comments:

Post a Comment