日期:2014-05-20  浏览次数:20729 次

敬求排列组合类算法,见描述
public   static   String[][]   arithmetic(ArrayList   list){

      String   arr=(String[])list.get(x)
    //list的每一项都是一个String[],list有多少项不定,String[]   有多少维不定
    //求数组间排列组合的结果


    //如
    list.get(0)=String[2]{ "1 ", "2 "};
    list.get(1)=String[3]{ "a ", "b ", "c "};
    //则返回:
    1   a
    1   b
    1   c
    2   a
    2   b
    2   c

      //如
    list.get(0)=String[2]{ "1 ", "2 "};
    list.get(1)=String[3]{ "a ", "b ", "c "};
    list.get(2)=String[2]{ "d ", "e "};

    //则返回:
    1   a   d
    1   a   e
    1   b   d
    1   b   e
    1   c   d
    1   c   e
   
    2   a   d
    2   a   e
    2   b   d
    2   b   e
    2   c   d
    2   c   e
 
}

------解决方案--------------------
import java.util.ArrayList;

public class Test {


public static void main(String[] args)
{
ArrayList list=new ArrayList();
list.add(new String[]{ "1 ", "2 "});
list.add(new String[]{ "a ", "b ", "c "});
list.add(new String[]{ "d ", "e "});

Test test=new Test();
test.visit(1,new StringBuffer( " "),list);
}

public void visit(int depth,StringBuffer lastLevelStringBuff,ArrayList inputList){
String[] currentLevelString=(String[])inputList.get(depth-1);
if(depth==inputList.size())
{
for(int i=0;i <currentLevelString.length;i++)
{
StringBuffer sb=new StringBuffer().append(lastLevelStringBuff);
System.out.println(sb.append(currentLevelString[i]));
}
}
else
{
for(int i=0;i <currentLevelString.length;i++)
{
StringBuffer sb=new StringBuffer().append(lastLevelStringBuff);
visit(depth+1,sb.append(currentLevelString[i]).append( " "),inputList);
}
}
}

}