Created
April 4, 2010 23:02
-
-
Save pcyu16/355785 to your computer and use it in GitHub Desktop.
all permutation example, using bit mask checking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class code | |
| { | |
| public static int pow2(int power) | |
| { | |
| int ret = 1; | |
| for(int t=0;t<power;t++) | |
| ret <<= 1; | |
| return ret; | |
| } | |
| public static void main(String [] args) | |
| { | |
| final int total = 3; | |
| final int [] num = {3,5,11}; | |
| boolean [] state = new boolean[total]; | |
| for(int x=1;x<code.pow2(total);x++) | |
| { | |
| for(int i=0;i<total;i++) | |
| { | |
| // if the ith bit of x is not 0 | |
| if( ( ( 1 << i ) & x ) != 0 ) | |
| state[i] = true; | |
| else | |
| state[i] = false; | |
| } | |
| String output = new String(); | |
| for(int i=0;i<total;i++) | |
| { | |
| if( state[i] == true ) | |
| output = output + num[i] + " "; | |
| } | |
| System.out.println(output); | |
| } | |
| System.exit(0); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
all permutation example
using bit mask checking