java 求陣列子集
給定陣列為{1,2,3,4,5,6}
輸出陣列中和為7的子集,每個子集以換行區分
package nuaa.ldm;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int[] flag = new int[100];
static int index = 0;
static int max = -1;
public static void numGroup(int[] arr, int start, int length, int sum) {
if (sum == 0) {
for (int j = 0; j < index; j++) {
System.out.print(flag[j])
}
System.out.println();
else {
for (int i = start; i < length; i++) {
flag[index++] = arr[i];
numGroup(arr, i + 1, length-1, sum - arr[i]);
}
}
index--;
}
public static void main(String[] args) {
int [] arr = {1,2,3,4,5,6};
Arrays.sort(arr);
numGroup(arr, 0, arr.length, 7);
System.out.println(max);
}
}