有个串类似这种:a1,a2,a3$b1,b2$c1,c2$
但是具体有几个$分开的不确定,每个$分开的域元素个数也就是a1,a2不确定,写个算法实现输出
a1,b1,c1
a1,b1,c2
a1,b2,c1
a1,b2,c2a2,b1,c1
a2,b1,c2
a2,b2,c1
a2,b2,c2a3,b1,c1
a3,b1,c2
a3,b2,c1
a3,b2,c2其实就是算不定个数的组合方式,用js写谢谢大家

解决方案 »

  1.   

    用3个链表存 a,b,c的数据然后遍历
    我是混C++的
      

  2.   

    lz 再加 20 分,俺就告诉你另,a b c 是能确定的吧?不会再有 d e f... 了吧?
      

  3.   

    public static void main(String[] args) {
    String s = "a1,a2,a3$b1,b2$c1,c2$d1,d2,d4$e1,e2";
    String[] group = s.split("\\$");
    int count = 0;
    String[] result = group[count].split(",");
    do {
    result = mul(result,group[count+1].split(","));
    count++;
    } while (count < group.length-1);
    for(String str:result){
    System.out.println(str);
    }
    }

    static String[] mul(String[] s1,String[] s2){
    List<String> temp = new ArrayList<String>();
    for(int i=0;i<s1.length;i++){
    for(int j=0;j<s2.length;j++){
    temp.add(s1[i]+","+s2[j]);
    }
    }
    return temp.toArray(new String[]{});
    }java版的
      

  4.   


    function mul(arr1,arr2){
        var temp = [];
        for(var i=0;i<arr1.length;i++){
            for(var j=0;j<arr2.length;j++){
                temp.push(arr1[i]+","+arr2[j]);
            }
        }
        return temp;
      }

    var tar = "a1,a2,a3$b1,b2$c1,c2$";
    var group = tar.split("\$");
    var count = 0;
      var result = group[count].split(",");
      do {
       if(group[count+1]){
       result = mul(result,group[count+1].split(","));
       }
          count++;
      } while (count < group.length-1);
      for(var str in result){
          document.write(result[str]+"<br>");
          document.close();
      }