我是C#的初學者,我正在尋找一個代碼來返回一組集合的所有可能的排列,例如{1,1,2},沒有重複{112,121,211}。我找到了以下鏈接,但我不知道如何使用它。無重複排列C#
http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G
我曾嘗試的代碼如下。例如,當我嘗試獲取「111」的排列組合時,它會返回所有可能的排列組合,即六個111。但我正在尋找一些提供排列而不重複的東西。更詳細地說,111只是一個排列而不是六個排列。
class Program
{
private static void Swap(ref char a, ref char b)
{
if (a == b) return;
a ^= b;
b ^= a;
a ^= b;
}
public static void GetPer(char[] list)
{
int x = list.Length - 1;
GetPer(list, 0, x);
}
private static void GetPer(char[] list, int k, int m)
{
if (k == m)
{
Console.Write(list);
}
else
for (int i = k; i <= m; i++)
{
Swap(ref list[k], ref list[i]);
GetPer(list, k + 1, m);
Swap(ref list[k], ref list[i]);
}
}
static void Main()
{
string str = "sagiv";
char[] arr = str.ToCharArray();
GetPer(arr);
}
}
先打在谷歌搜索 - http://stackoverflow.com/questions/756055/listing-all-permutations-of-a-string-integer – JamieMeyer
我試過了,但是當我測試「11123344」或「111」 –
{1,1,2}不是一組時,它會返回錯誤的解決方案。 –