C#輸出字符串組合排列

比如“你好啊”輸出結果如圖:有6種組合

代碼:

        static int count;
        static void Combine(string str, string str2)
        {
            if (str == null)
                return;
            if (str == string.Empty)
            {
                Console.WriteLine(str2);
                count++;
            }
            for (int i = 0; i < str.Length; i++)
            {
                Combine(str.Remove(i, 1), str2 + str[i].ToString());
            }
        }

調用方法:

string str2 = "";
Combine("abcd", str2);

輸出結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章