日期:2014-05-18  浏览次数:20880 次

数组的排列组合问题
public static void Main(string [] args)  
{  
string[] str = new string[3];
str[0] = "12,34";
str[1] = "c";
str[2] = "d,f";
Perm(str);
}  
 

static void Perm(params string [] data)  
{  
int[] index = new int[data.Length];  
int i = 0;  
while(true)  
{  
char[] buf = new char[index.Length];  
for(i = 0; i < index.Length; i++)  
{  
buf[i] = data[i][index[i]];  
}  
Console.WriteLine(new string(buf));  
i = index.Length - 1;  
while(i >= 0)  
{  
index[i]++;  
if(index[i] < data[i].Length) break;  
index[i--] = 0;  
}  
if(i < 0) return;

}
}
我想得到12,c,d
12,c,f
34,c,d
34,c,f
但是我从网上找的这个代码总把12,34拆开
那位能帮帮我啊!

------解决方案--------------------
C# code

        static void Main(string[] args)
        {
            string[] str = new string[3];
            str[0] = "12,34";
            str[1] = "c,d";
            str[2] = "d,f";
            Matrix m = new Matrix(str[0].Split(','));
            for (int i = 1; i < str.Length; i++)
            {
                m = m.Multiply(new Matrix(str[i].Split(',')));
            }
            foreach (string s in m.element)
            {
                Console.WriteLine(s);
            }
        }
        struct Matrix
        {
            public string[] element;
            public Matrix(string[] s)
            {
                element = s;
            }
            public Matrix Multiply(Matrix m)
            {
                string[] s = new string[this.element.Length * m.element.Length];
                int index = 0;
                for (int i = 0; i < this.element.Length; i++)
                {
                    for (int j = 0; j < m.element.Length; j++)
                    {
                        s[index] = this.element[i] + "," + m.element[j];
                        index++;
                    }
                }
                return new Matrix(s);
            }
        }