我在使用一个功能而不是使用多个功能时遇到了一些麻烦。
如果我想获得像 2^3 这样的重复排列。 重复排列
要得到:
000
001
101
011
100
101
110
111
我可以有这个功能:
static void Main(string[] args)
{
three_permutations(2);
Console.ReadLine();
}
static void three_permutations(int y)
{
for (int aa = 0; aa < y; aa++)
{
for (int bb = 0; bb < y; bb++)
{
for (int cc = 0; cc < y; cc++)
{
Console.Write((aa));
Console.Write((bb));
Console.Write((cc));
Console.WriteLine();
}
}
}
}
但是然后做4(比如2 ^ 4),我能想到的唯一方法是:
static void four_permutations(int y)
{
for (int aa = 0; aa < y; aa++)
{
for (int bb = 0; bb < y; bb++)
{
for (int cc = 0; cc < y; cc++)
{
for (int dd = 0; dd < y; dd++)
{
Console.Write((aa));
Console.Write((bb));
Console.Write((cc));
Console.Write((dd));
Console.WriteLine();
}
}
}
}
}
但我确信使用递归有更好的方法我只是不知道该怎么做。我很感激任何帮助。谢谢。