例如  a b c d
2个字符要组合 ab  ac  ad   
       ba  bc  bd 
       ca  cb  cd 
       da  db  dc  
3个字符 组合  以此类推 怎么个思路  

解决方案 »

  1.   

    http://topic.csdn.net/u/20090217/21/f41ed9f6-f929-451c-a5c9-80d2e408422a.html
      

  2.   


            static void Main(string[] args)
            {
                List<string> retVal = Output(new char[] { 'a', 'b', 'c', 'd' });            foreach (string item in retVal)
                {
                    Console.WriteLine(item);
                }            Console.WriteLine();
                Console.WriteLine("done");
                Console.ReadLine();
            }
            static List<string> Output(char[] array)
            {
                if (array == null)
                {
                    throw new ArgumentNullException("array");
                }
                if (array.Length == 0)
                {
                    return null;
                }            List<string> retVal = new List<string>();
                if (array.Length == 1)
                {
                    retVal.Add(array[0].ToString());
                    return retVal;
                }            foreach (char first in array)
                {
                    retVal.Add(first.ToString());                char[] subArray = RemoveItem(array, first);
                    List<string> subRet = Output(subArray);
                    for (int i = 0; i < subRet.Count; i++)
                    {
                        subRet[i] = first + subRet[i];
                    }                retVal.AddRange(subRet);
                }            return retVal;
            }        static char[] RemoveItem(char[] array, char removeItem)
            {
                char[] retVal = new char[array.Length - 1];
                int i = 0;
                foreach (char item in array)
                {
                    if (item != removeItem)
                    {
                        retVal[i] = item;
                        i++;
                    }
                }
                return retVal;
            }
      

  3.   


     static void Main(string[] args)
            {
                List<int> InPut = new List<int> { 1, 2,3 };//输入数组
                List<int> Setting = new List<int>();
                int Replicate =2;//个数
                int iResult = 0;
                Loop(InPut, Setting, Replicate, ref iResult);
                Console.Read();
            }        static void Loop(List<int> InPut, List<int> Setting, int Replicate, ref int count)
            {
                if (Setting.Count() ==  Replicate)//all setting
                {
                    count++;
                    //打印结果
                    foreach (int cur in Setting)
                    {
                        Console.Write(cur.ToString() + " ");
                    }
                    Console.WriteLine();
                }
                else
                {
                    foreach (int haha in InPut)
                    {
                        int iCount = Setting.Count(item => item == haha);
                        if (iCount ==0 )
                        {
                            Setting.Add(haha);
                            Loop(InPut, Setting, Replicate, ref count);//nested call
                            Setting.RemoveAt(Setting.Count - 1);//remove the last element
                        }
                    }
                }
            }
      

  4.   

    N个样本空间:List<string> strs,存储样本我建议用ascii做,这样效率会更高。
    以2个字符排列为例:
    for(int i=0;i<n;i++)
    {
      str=strs[i];
      for (int j=1;j<n;j++)
      {
        result += str + (string)(str的ascii码+j) + ",";
      }
      for (int j=1;j<=i;j++)
      {
        result += str + (string)(str的ascii码-j) + ",";
      }}大概写的
      

  5.   

    挺简单的啊 
    string[] s1 = new string[] { "a", "b", "c", "d" };
                string[] s2 = s1;
                for (int i = 0; i < s1.Length; i++)
                {
                    for (int j = 0; j < s2.Length; j++)
                    {
                        if(s1[i]!=s2[j])
                        Console.Write(s1[i]+s2[j]);
                    }
                    Console.WriteLine();
                }
      

  6.   

    c#
    ------------ string[] s1 = new string[] { "a", "b", "c", "d" };
                string[] s2 = s1;
                string[] s3 = s1;
                for (int i = 0; i < s1.Length; i++)
                {
                    for (int j = 0; j < s2.Length; j++)
                    {
                        for (int k = 0; k < s3.Length; k++)
                        {
                            if (s1[i] != s2[j] && s1[i] != s3[k] && s2[j] != s3[k])
                                Console.WriteLine(s1[i] + s2[j]+s3[k]);
                        }
                    }
                    Console.WriteLine();
                }
                Console.ReadKey();
      

  7.   

    参考下面的内容,做了修改        static string[] m_Data = { "A", "B", "C", "D", "E" };        static void Main(string[] args)
            {
                Dictionary<string, int> dic = new Dictionary<string, int>();
                for (int i = 0; i < m_Data.Length; i++)
                {
                    Console.WriteLine(m_Data[i]);//如果不需要打印单元素的组合,将此句注释掉
                    dic.Add(m_Data[i], i);
                }
                GetString(dic);
                Console.ReadLine();
            }        static void GetString(Dictionary<string, int> dd)
            {
                Dictionary<string, int> dic = new Dictionary<string, int>();
                foreach (KeyValuePair<string, int> kv in dd)
                {
                    for (int i = 0; i < kv.Value; i++)
                    {
                        Console.WriteLine(kv.Key + m_Data[i]);
                        //dic.Add(kv.Key + m_Data[i], i);
                    }
                    for (int i = kv.Value + 1; i < m_Data.Length; i++)
                    {
                        Console.WriteLine(kv.Key + m_Data[i]);
                        dic.Add(kv.Key + m_Data[i], i);
                    }
                }
                if (dic.Count > 0) GetString(dic);
            }
      

  8.   

    实现了ba ca cb da db dc的加入。