C# 手动终止递归
我写了一个递归,实际上我在网上找到了递归,可以得到一组数字的所有可能排列,但在某些情况下,由于可能排列的数量很大,我想添加一个If语句,在递归进行所有排列之前终止递归。我试着输入一个返回语句,但似乎不起作用 我在编码方面有点新手,所以如果答案对每个人来说都很明显,我很抱歉,我就是无法得到它C# 手动终止递归,c#,recursion,C#,Recursion,我写了一个递归,实际上我在网上找到了递归,可以得到一组数字的所有可能排列,但在某些情况下,由于可能排列的数量很大,我想添加一个If语句,在递归进行所有排列之前终止递归。我试着输入一个返回语句,但似乎不起作用 我在编码方面有点新手,所以如果答案对每个人来说都很明显,我很抱歉,我就是无法得到它 class EntryPoint { static void Main() { //input an initial sequence o
class EntryPoint
{
static void Main()
{
//input an initial sequence of the trains to schedule
Console.Write("Input train permutation");
string inputLine = Console.ReadLine();
GenerateTrainOrder GTO = new GenerateTrainOrder();
GTO.InputSet = GTO.MakeCharArray(inputLine);
GTO.CalcPermutation(0);
}
}
class GenerateTrainOrder
{
private int elementLevel = -1; // elements examined iterates immediately after the CalcPermutation initiates so we must set it equal -1 to start from 0
private int[] permutationValue = new int[0];
public int[,] Paths = new int[ParametersClass.timetableNumber, ParametersClass.trainsToSchedule];
private char[] inputSet;
public char[] InputSet
{
get { return inputSet; }
set { inputSet = value; }
}
private int permutationCount = 0;
public int PermutationCount
{
get { return permutationCount; }
set { permutationCount = value; }
}
//transform the input from the console to an array for later use
public char[] MakeCharArray(string InputString)
{
char[] charString = InputString.ToCharArray();
Array.Resize(ref permutationValue, charString.Length);
return charString;
}
public void CalcPermutation(int k)
{
elementLevel++;
permutationValue.SetValue(elementLevel, k);
//if we have gone through all the elements which exist in the set, output the results
if (elementLevel == ParametersClass.trainsToSchedule)
{
OutputPermutation(permutationValue); // output TrainOrder by passing the array with the permutation
}
//if there are elements which have not been allocated a place yet
else
{
for (int i = 0; i < ParametersClass.trainsToSchedule; i++)
{
//iterate until we come upon a slot in the array which has not been allocated an elements yet
if (permutationValue[i] == 0)
{
CalcPermutation(i); //rerun the code to allocate an element to the empty slot. the location of the empty slot is given as a parameter (this is how k increments)
}
}
}
elementLevel--;
permutationValue.SetValue(0, k);
}
private void OutputPermutation(int[] value)
{
int slot = 0;
foreach (int i in value)
{
Paths[permutationCount, slot] = Convert.ToInt16(Convert.ToString(inputSet.GetValue(i-1)));
slot++;
}
PermutationCount++;
}
}
停止递归计算有点棘手。如果有对象,最好的方法是使用全局变量或私有类变量 此变量表示递归是否应停止:
bool stopRecursion = false;
现在,在您的计算中,您将在每个步骤后更新此变量:
if(_my_condition_is_met) stopRecursion = true;
在递归调用的方法开始时,检查此变量的状态:
public void CalcPermutation(int k)
{
if(stopRecursion) return;
...
在每次调用CalcPermutation后检查此变量:
使用这种方法,只要满足停止条件,就可以立即释放深层调用级别。停止递归计算有点棘手。如果有对象,最好的方法是使用全局变量或私有类变量 此变量表示递归是否应停止:
bool stopRecursion = false;
现在,在您的计算中,您将在每个步骤后更新此变量:
if(_my_condition_is_met) stopRecursion = true;
在递归调用的方法开始时,检查此变量的状态:
public void CalcPermutation(int k)
{
if(stopRecursion) return;
...
在每次调用CalcPermutation后检查此变量:
使用此方法,您可以在满足停止条件后立即解除深层呼叫级别。谢谢大家的回复。我通过以下方式成功地实现了这一目标: 在Calcpermutati行下,我写道
if (permutationCount == ParametersClass.timetableNumber)
{
return;
}
我试着用不同的输入运行了几次,似乎效果很好。谢谢大家的回复。我通过以下方式成功地实现了这一目标: 在Calcpermutati行下,我写道
if (permutationCount == ParametersClass.timetableNumber)
{
return;
}
我试着用不同的输入运行了几次,它似乎运行得很好。终止条件是什么,您试着把return放在哪里?你什么意思好像不起作用?return将始终终止函数并返回;终止条件是什么?您尝试将退货放在哪里?你什么意思好像不起作用?return将始终终止函数并返回;首先,如果递归函数只返回一个结果来通知它必须停止,则不需要全局变量或字段。这在多线程代码中尤其重要。第二,.NET 4.5有CancellationToken类来表示它应该取消的任何类型的函数。@PanagiotisKanavos OP说我有点新手,所以这里的取消标记可能有点太多了。返回值当然是一个很好的主意。他谈到停止递归而不是退出方法。语言是important@Hellraiser如何停止递归而不返回?For循环本身就是一个递归。Break停止递归。Return退出该方法。所以请正确使用术语首先,如果递归函数只返回一个结果,通知它必须停止,则不需要全局变量或字段。这在多线程代码中尤其重要。第二,.NET 4.5有CancellationToken类来表示它应该取消的任何类型的函数。@PanagiotisKanavos OP说我有点新手,所以这里的取消标记可能有点太多了。返回值当然是一个很好的主意。他谈到停止递归而不是退出方法。语言是important@Hellraiser如何停止递归而不返回?For循环本身就是一个递归。Break停止递归。Return退出该方法。所以请正确使用术语