标签:ar sp java for 2014 bs amp as nbsp
package com.xsz.demo; /** * @author cwqi * @date 2014-11-19 下午9:55:31 */ public class SubSet { /** * 求集合的子集 * @param args */ public static void main(String[] args) { int array[] = {1,2,3}; SubSet.getSubSet(array); } private static void getSubSet(int[] array) { int len = array==null? 0:array.length; int total = (int)Math.pow(2,len)-1;//2的len次方-1,即子集的個數 int factor = (int)Math.pow(2,len-1);//2的len-1次方 for (int i = 0; i <=total; i++) { //subSetIndex轉成二進制后,1對應的array下標的元素為這個子集的元素 //如0101,對應的子集為{24} int subSetIndex =i; //輸出一個子集 int index = 0; System.out.print("{"); while (subSetIndex != 0) { if ((subSetIndex & factor) ==factor) { System.out.print(array[index]); } subSetIndex = subSetIndex<<1;//左移一位 index ++; } System.out.print("}"); System.out.println(); } } }
标签:ar sp java for 2014 bs amp as nbsp
原文地址:http://my.oschina.net/u/1861837/blog/347075