static void possible_combination(int[] a, int k) {
if (k == a.length) {
for (int i = 0; i < a.length; i++) {
System.out.print(" [" + a[i] + "] ");
}
System.out.println();
} else {
for (int i = k; i < a.length; i++) {
int temp = a[k];
a[k] = a[i];
a[i] = temp;
possible_combination(a, k + 1);
temp = a[k];
a[k] = a[i];
a[i] = temp;
}
}
}
函数调用是:possible_combination(array, 0);
如果我给输入数组包含像1 2 3这样的元素,我输出像
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
在这个输出中,我必须得到像第n行值的元素。例如,如果我给3,我需要3&lt; rd行值2 1 3。
答案 0 :(得分:0)
import java.util.*;
class Array13
{
public static void main(String args[])
{
int arr[][]={{1,2,3},
{1,3,2},
{2,1,3},
{2,3,1},
{3,1,2},
{3,2,1}};
System.out.println("Enter the row number :");
Scanner sc=new Scanner(System.in);
int r = sc.nextInt();
printVal(arr,r-1);
}
public static void printVal(int mat[][],int row)
{
for(int i=0;i<mat.length;i++)
{
if(i == row)
{
for(int j=0;j<mat[row].length;j++)
{
System.out.print(mat[i][j]);
System.out.print(" ");
}
}
}
}
}