请教如何把static void combinationUtil() 取消, 然后合成到 public static void main (String[] args), 或者该如何 在public static void main (String[] args) 里面做一个双数组 data2[][], 然后把 combinationUtil()里面的 data[] 存到 data2[][]
class permutation {
static void combinationUtil(int arr[], int data[], int start, int end, int index, int r)
{
if (index == r)
{
for (int j=0; j<r; j++)
System.out.print(data[j]+" ");
System.out.println("");
return;
}
for (int i=start; i<=end && end-i+1 >= r-index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r);
}
}
public static void main (String[] args) {
int arr[] = {1, 2, 3, 4, 5,6,7};
int r = 5;
int n = arr.length;
int data[]=new int[r];
int start=0,end=n-1,index=0;
combinationUtil(arr, data, start, end, index, r);
}
}
程序运行的结果是
1 2 3 4 5
1 2 3 4 6
1 2 3 4 7
1 2 3 5 6
1 2 3 5 7
1 2 3 6 7
1 2 4 5 6
1 2 4 5 7
1 2 4 6 7
1 2 5 6 7
1 3 4 5 6
1 3 4 5 7
1 3 4 6 7
1 3 5 6 7
1 4 5 6 7
2 3 4 5 6
2 3 4 5 7
2 3 4 6 7
2 3 5 6 7
2 4 5 6 7
3 4 5 6 7