有编号分别为0,1,2,3,4的5本书,准备分给5个人A,B,C,D,E,每个人阅读兴趣用一个二维数组加以描述,公式如下:   
                  1   i喜欢j书
    Like[i][j] = 
                  0   i不喜欢j书
写一个程序,输出所有分书方案,让人人皆大欢喜。假定5个人对5本书的阅读兴趣如下图所示。 
人\书 0 1 2 3 4
A 0 0 1 1 0
B 1 1 0 0 1
C 0 1 1 0 1
D 0 0 0 1 0
E 0 1 0 0 1
我能够输出一个方案  但是有谁能告诉我输出所有方案怎么实现呢
   怎么样判断一个方案已经输出过呢?用一个数组存起来?

解决方案 »

  1.   

    package com.wisage.june;import java.util.ArrayList;
    import java.util.List;public class Interest { static String[] PERSON = { "alex", "bob", "carey", "dick", "elapse" }; static String[] BOOK = { "8thManage",
    "your imamage is worthy of 100 billion pounds", "people", "chinese",
    "Englsih" }; int[][] option = { 
    { 0, 0, 1, 1, 0 }, 
    { 1, 1, 0, 0, 1 }, 
    { 0, 1, 1, 0, 1 },
    { 0, 0, 0, 1, 0 }, 
    { 0, 1, 0, 0, 1 } }; static int LEN = 5; public static void main(String[] args) {
    Interest hand = new Interest();
    List list = hand.solution(LEN);
    } public List solution(int len) {
    int[][] bookOption = new int[len][];
    for (int bookIndex = 0; bookIndex < len; bookIndex++) {
    bookOption[bookIndex] = new int[selected(bookIndex)];
    int count = 0;
    for (int personIndex = 0; personIndex < len; personIndex++) {
    if (option[personIndex][bookIndex] == 1) {
    bookOption[bookIndex][count++] = personIndex;
    }
    }
    } List list = new ArrayList();
    int count = 0;
    StringBuffer sol = new StringBuffer();
    String[] sols = new String[len];
    for (int a = 0; a < bookOption[0].length; a++) {
    sol = new StringBuffer();
    int indexA = bookOption[0][a];
    sols[0] = PERSON[indexA];
    for(int b =0; b <bookOption[1].length; b++){
    int indexB = bookOption[1][b];
    if(indexB == indexA) continue;
    sols[1] = PERSON[indexB];
    for(int c =0; c <bookOption[2].length; c++){
    int indexC = bookOption[2][c];
    if(indexC == indexA || indexC == indexB) continue;
    sols[2] = PERSON[indexC];
    for(int d =0; d <bookOption[3].length; d++){
    int indexD = bookOption[3][d];
    if(indexD == indexA || indexD == indexB || indexD == indexC) continue;
    sols[3] = PERSON[indexC];
    for(int e =0; e <bookOption[4].length; e++){
    int indexE = bookOption[4][e];
    if(indexE == indexA || indexE == indexB || indexE == indexC|| indexE == indexD) continue;
    sols[4] = PERSON[indexE];
    list.add(sols.clone());
    Conica.pr("Solution " + count ++ + ":");
    Conica.pr(sols);
    }
    }
    }
    }
    }
    return list;
    } public int selected(int bookIndex) {
    int sum = 0;
    for (int i = 0; i < LEN; i++) {
    if (option[i][bookIndex] == 1)
    sum++;
    }
    return sum;
    }
    }我来凑个热闹,楼主可参考一下。
      

  2.   

    笨蛋,你把Conica去掉不就可以了,那是我写的一个输出类。
    package com.wisage.june;import java.util.*;public class Conica { public static void main(String[] args) {
    } public static void pr(char c) {
    System.out.print(c);
    } public static void pl(char c) {
    System.out.println(c);
    } public static void print(char c) {
    System.out.print(c);
    } public static void println(char c) {
    System.out.println(c);
    } public static void pr(String s) {
    System.out.print(s);
    } public static void pl(StringBuffer s) {
    System.out.println(s);
    } public static void pr(StringBuffer s) {
    System.out.print(s);
    } public static void pl(char[] c) {
    for (int i = 0; i < c.length; i++) {
    System.out.println("index " + i + ":" + c[i]);
    }
    } public static void pl(Object obj) {
    System.out.println(obj);
    } public static void pr(Object obj) {
    System.out.print(obj);
    } public static void pl(List list) {
    System.out.println(list);
    } public static void pr(List list) {
    System.out.print(list);
    } public static void pl(String s) {
    System.out.println(s);
    } public static void pl() {
    System.out.println();
    } public static void pr() {
    System.out.print(" ");
    } public static void pr(boolean b) {
    System.out.print(b);
    } public static void pl(boolean b) {
    System.out.println(b);
    } public static void pr(int i) {
    System.out.print(i);
    } public static void pl(int i) {
    System.out.println(i);
    } public static void pr(double d) {
    System.out.print(d);
    } public static void pr(Float F) {
    System.out.print(F.floatValue());
    } public static void pl(Float F) {
    System.out.println(F.floatValue());
    } public static void pl(double d) {
    System.out.println(d);
    } public static void println(Object obj) {
    System.out.println(obj);
    } public static void print(Object obj) {
    System.out.print(obj);
    } public static void print(int i) {
    System.out.print(i);
    } public static void printTable(List list) {
    HashMap map;
    int size = list.size();
    for (int i = 0; i < size; i++) {
    map = (HashMap) list.get(i);
    print(map);
    System.out.println();
    }
    } public static void print(HashMap map) {
    Iterator it = map.keySet().iterator();
    int size = map.size();
    Object key;
    Object value;
    for (int i = 0; i < size; i++) {
    key = it.next();
    value = map.get(key);
    if (i + 1 == size)
    System.out.print(key + ":" + value);
    else
    System.out.print(key + ":" + value + ",");
    }
    } public static void println(HashMap map) {
    Iterator it = map.keySet().iterator();
    Object key;
    Object value;
    for (int i = 0; i < map.size(); i++) {
    key = it.next();
    value = map.get(key);
    System.out.println("key:" + key);
    System.out.println(" value:" + value);
    }
    } public static void println(Object[] strS) {
    for (int i = 0; i < strS.length; i++) {
    System.out.println("index " + i + ":" + strS[i]);
    }
    } public static void pl(Object[] strS) {
    for (int i = 0; i < strS.length; i++) {
    System.out.println("index " + i + ":" + strS[i]);
    }
    } public static void pl(byte[][] pos) {
    for (int i = 0; i < pos.length; i++) {
    Conica.pr(pos[i]);
    }
    } public static void pl(int[][] pos) {
    for (int i = 0; i < pos.length; i++) {
    Conica.pr(pos[i]);
    }
    } public static void pr(Object[] strS) {
    for (int i = 0; i < strS.length; i++) {
    System.out.print(strS[i]);
    if(i != strS.length - 1)
    System.out.print(",");
    }
    System.out.println();
    } public static void println(int[] ints) {
    for (int i = 0; i < ints.length; i++) {
    System.out.println("index " + i + ":" + ints[i]);
    }
    } public static void pl(int[] ints) {
    for (int i = 0; i < ints.length; i++) {
    System.out.println("index " + i + ":" + ints[i]);
    }
    } public static void pr(int[] ints) {
    for (int i = 0; i < ints.length; i++) {
    System.out.print(ints[i]);
    if(i!=ints.length - 1){
    System.out.print(",");
    }
    }
    } public static void println(byte[] ints) {
    for (int i = 0; i < ints.length; i++) {
    System.out.println("index " + i + ":" + ints[i]);
    }
    } public static void pl(byte[] ints) {
    for (int i = 0; i < ints.length; i++) {
    System.out.println("index " + i + ":" + ints[i]);
    }
    } public static void pr(byte[] ints) {
    int len = ints.length;
    for (int i = 0; i < len; i++) {
    if (i + 1 == len)
    System.out.println(ints[i]);
    else
    System.out.print(ints[i] + ",");
    }
    } public static void print(int[] ints) {
    int len = ints.length;
    for (int i = 0; i < len; i++) {
    if (i + 1 == len)
    System.out.println(ints[i]);
    else
    System.out.print(ints[i] + ",");
    }
    } public static void print(Properties prop) {
    Iterator it = (prop.keySet()).iterator();
    while (it.hasNext()) {
    String key = (String) it.next();
    Object value = prop.get(key);
    System.out.println("key=" + key + ",value=" + value);
    }
    }
    }