比如“你好啊”輸出結果如圖:有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);
輸出結果: