使用Google查找示例后,我找到了following example goes 用于生成排列:
namespace ConsoleApp1
{
class Program
{
public static void Main()
{
int n, i;
formPermut test = new formPermut();
int[] arr1 = new int[5];
Console.WriteLine("\n\n Recursion : Generate all possible permutations of an array :");
Console.WriteLine("------------------------------------------------------------------");
Console.Write(" Input the number of elements to store in the array [maximum 5 digits ] :");
n = Convert.ToInt32(Console.ReadLine());
Console.Write(" Input {0} number of elements in the array :\n", n);
for (i = 0; i < n; i++)
{
Console.Write(" element - {0} : ", i);
arr1[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("\n The Permutations with a combination of {0} digits are : \n", n);
test.prnPermut(arr1, 0, n - 1);
Console.Write("\n\n");
Console.ReadKey();
}
class formPermut
{
public void swapTwoNumber(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
public void prnPermut(int[] list, int k, int m)
{
int i;
if (k == m)
{
for (i = 0; i <= m; i++)
Console.Write("{0}", list[i]);
Console.Write(" ");
}
else
for (i = k; i <= m; i++)
{
swapTwoNumber(ref list[k], ref list[i]);
prnPermut(list, k + 1, m);
swapTwoNumber(ref list[k], ref list[i]);
}
}
}
}
}
所以,如果我有 2 个输入值 1
和 2
,上面的代码将返回以下结果 12
和 21
。
我需要它返回更多的结果,例如:
1
12
2
21
如果我输入 3 个值 1
和 2
和 3
,此时它返回:
123 132 213 231 321 312
但我需要它同时返回所有两位数和一位数的排列。
有人知道我该怎么做吗?
例如:
1 2 3 12 13 21 23 31 32 123 132 213 321 312
等等,我可能漏掉了一些组合/排列。
我的最终目标是能够做同样的事情,但是使用字符串,所以如果输入是 one
和 two
,输出将是:
一个
onetwo
two
twoone
对于三个输入,例如:
一
和二
和三
输出将是:
一二三一二一三二一二三三一三二一二三一二二一三三二一三一二
假设我没有错过任何组合。
最佳答案
这似乎可以解决问题?
测试用例:
输入:1,2 给出...
2 1 21 12
输入 1、2、3 给出...
3 2 32 23 1 31 13 21 12 321 312 231 213 123 132
输入“一”、“二”、“三”给出...
three two threetwo twothree one threeone onethree twoone onetwo threetwoone threeonetwo twothreeone twoonethree onetwothree onethreetwo
来自这段代码:
class Program
{
public static void Main()
{
formPermut test = new formPermut();
test.prnPermutWithSubsets(new object[] { 1, 2 });
Console.WriteLine();
test.prnPermutWithSubsets(new object[] { 1, 2, 3 });
Console.WriteLine();
test.prnPermutWithSubsets(new string[] { "one", "two", "three" });
Console.WriteLine();
return;
}
class formPermut
{
private void swapTwoNumber(ref object a, ref object b)
{
object temp = a;
a = b;
b = temp;
}
public void prnPermutWithSubsets(object[] list)
{
for (int i = 0; i < Math.Pow(2, list.Length); i++)
{
Stack<object> combination = new Stack<object>();
for (int j = 0; j < list.Length; j++)
{
if ((i & (1 << (list.Length - j - 1))) != 0)
{
combination.Push(list[j]);
}
}
this.prnPermut(combination.ToArray(), 0, combination.Count() - 1);
}
}
public void prnPermut(object[] list, int k, int m)
{
int i;
if (k == m)
{
for (i = 0; i <= m; i++)
Console.Write("{0}", list[i]);
Console.Write(" ");
}
else
for (i = k; i <= m; i++)
{
swapTwoNumber(ref list[k], ref list[i]);
prnPermut(list, k + 1, m);
swapTwoNumber(ref list[k], ref list[i]);
}
}
}
}
关于c# - 超过你的正常排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54243488/