有五本书,它们的编号分别为1,2,3,4,5,现准备分给 A, B, C, D, E五个人,每个人的阅读兴趣用一个二维数组来加以描述:
希望编写一个程序,输出所有的分书方案,让人人皆大欢喜。
1 import java.math.BigInteger; 2 import java.util.Arrays; 3 import java.util.Scanner; 4 5 6 public class Main { 7 public static int[][] a = new int[6][6]; 8 public static int n = 5; 9 public static int[] b = new int[6]; 10 public static void main(String[] args) { 11 a[1][3] = 1; 12 a[1][4] = 1; 13 a[2][1] = 1; 14 a[2][2] = 1; 15 a[2][5] = 1; 16 a[3][2] = 1; 17 a[3][3] = 1; 18 a[3][5] = 1; 19 a[4][4] = 1; 20 a[5][2] = 1; 21 a[5][5] = 1; 22 int[] c = new int[5]; 23 f(1,c,0); 24 } 25 public static void f(int i,int[] c,int h){ 26 if(i>5){ 27 System.out.println(Arrays.toString(c)); 28 29 }else{ 30 for(int j=1;j<=5;j++){ 31 if(a[j][i]==1&&b[j]==0){ 32 b[j] = 1; 33 c[h] = j; 34 f(i+1,c,h+1); 35 b[j] = 0; 36 c[h] = 0; 37 } 38 } 39 } 40 41 } 42 }