c#-列出字符串的所有排列和组合
如何获得字符串数组的组合和变体? 比如说A,B,C,D 期望得到这样的结果,它能够计算出AB和BA视图作为不同的组合c#-列出字符串的所有排列和组合,c#,C#,如何获得字符串数组的组合和变体? 比如说A,B,C,D 期望得到这样的结果,它能够计算出AB和BA视图作为不同的组合 A AB AC ABC ABCD ACBD ... B BA BC BCD BADC ... 编辑: 目前,我尝试的是使用列表的下面的代码,我在想是否应该为我的字符串集进行另一种排列(如set=abcd,bcda,cdab…),以获得完整的列表 string set = "abcd"; // Init list List<string>
A
AB
AC
ABC
ABCD
ACBD
...
B
BA
BC
BCD
BADC
...
编辑: 目前,我尝试的是使用列表的下面的代码,我在想是否应该为我的字符串集进行另一种排列(如
set=abcd,bcda,cdab…
),以获得完整的列表
string set = "abcd";
// Init list
List<string> subsets = new List<string>();
// Loop over individual elements
for (int i = 1; i < set.Length; i++)
{
subsets.Add(set[i - 1].ToString());
List<string> newSubsets = new List<string>();
// Loop over existing subsets
for (int j = 0; j < subsets.Count; j++)
{
string newSubset = subsets[j] + set[i];
newSubsets.Add(newSubset);
}
subsets.AddRange(newSubsets);
}
// Add in the last element
subsets.Add(set[set.Length - 1].ToString());
subsets.Sort();
string set=“abcd”;
//初始化列表
列表子集=新列表();
//在单个元素上循环
for(int i=1;i
由于我发现很难理解您的问题,我为我有限的回答道歉。不过我会尝试一下。我试图用C语言编写一个程序,该程序将显示类似于字符串的字符数组的组合,只是没有“\0”。我将尝试将代码转换为C#
要枚举的组合的/*字符串*/
char[]set={'a','b','c','d'};
/*数组的长度*/
int setLength=set.Length,a,b,c;
/*这将打印所有具有1个值的组合。例如A、B、C、D*/
对于(a=0;a
输出:
输出将不同,因为我将其更改为适合单个屏幕截图
我希望这有帮助
编辑:我发现一个类似的问题已经得到了回答:请仔细查看,因为它涵盖了这个问题中非常相似的主题。您尝试过什么吗?你不能不费吹灰之力就发布需求,期待帮助或有人帮你完成工作。Eric Lippert有一些关于制作的博客文章,你可能想读一下。我确实读过很多不同的帖子,比如说很好,但仍然不能满足我的需要,因为我还需要字符串的子集,像AB和BA一样,必须将视图视为不同的字符串$
/* String for combos to be enumerated */
char[] set = { 'a', 'b', 'c', 'd' };
/* Length of the array */
int setLength = set.Length, a, b, c;
/* This will print all combos that have 1 value. E.g. A, B, C, D */
for (a = 0; a < setLength; a++)
{
Console.WriteLine(set[a] + Environment.NewLine);
}
/* This will give the 1st value of the combo */
for (a = 0; a < setLength; a++)
{
/* This will give the 2nd value. Resulting in combos with a length of 2 */
for (b = 0; b < setLength; b++)
{
Console.WriteLine(set[a] + set[b] + Environment.NewLine);
}
}
/* 1st value */
for (a = 0; a < setLength; a++)
{
/* 2nd value */
for (b = 0; b < setLength; b++)
{
/* 3rd value */
for (c = 0; c < setLength; c++)
{
Console.WriteLine(set[a] + set[b] + set[c] + Environment.NewLine);
}
}
}
/* To continue with longer combos simply add more and more for loops */