用c#進行遞迴組合

iDotNetSpace發表於2009-02-18

    static string[] m_Data = { "A", "B", "C", "D", "E" };

        static void Main(string[] args)
        {
            Dictionary dic = new Dictionary();
            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 dd)
        {
            Dictionary dic = new Dictionary();
            foreach (KeyValuePair kv in dd)
            {
                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);
        }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-555234/,如需轉載,請註明出處,否則將追究法律責任。

相關文章