问题遇到的现象和发生背景
C#有多个返回值的函数如何实现递归
用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
public static List<OrderData> nextpoint(List<NewTrackData> currentlist, int currentpoint, NewTrackData currentpoint2)//当前list,当前点
{
double distence;
double minidistence;
Distencelist.Clear();
for (int i = 0; i < currentlist.Count; i++)//去掉第一个点的新list
{
distence = Math.Pow((currentlist[i].Track.Start_X - currentpoint2.Track.Start_X), 2.0) + Math.Pow((currentlist[i].Track.Start_Y - currentpoint2.Track.Start_Y), 2.0);
Distencelist.Add(distence);
}
minidistence = Distencelist.Min();//获取list中的最小值
List<int> minilist = new List<int>();
int minIndex = Distencelist.IndexOf(minidistence);//获取最小值的索引集合
for (int i = minIndex; i < Distencelist.Count; i++)
{
if (Distencelist[i] == minidistence)
{
minilist.Add(i);
}
}
List<int> index = new List<int>();
for (int i = 0; i < minilist.Count; i++)
{
index.Add(currentlist[minilist[i]].originalIndex);
}
List<NewTrackData> newlistreproduct = new List<NewTrackData>();
newlistreproduct = currentlist.GetRange(0, currentlist.Count);
OrderData temp = new OrderData();
List<OrderData> OrderData = new List<OrderData>();
temp.orderlist = index.GetRange(0, index.Count);
OrderData.Add(temp);
if (currentlist.Count == 0)
{
return OrderData;
}
else
{
for (int i = 0; i < index.Count; i++)
{
NewTrackData tempcurrentpoint = new NewTrackData();
tempcurrentpoint = newlistreproduct[index[i]];
newlistreproduct.RemoveAt(index[i]);
List<OrderData> OrderData2 = nextpoint(newlistreproduct, index[i], tempcurrentpoint);
OrderData.Add(OrderData2[i]);
}
return OrderData;
}
}
运行结果及详细报错内容
我的解答思路和尝试
函数返回值可能是多个值,或一个值,如何设计递归?上述函数的思路是:求出list数组中剩余项中距离当前项最小距离,然后去除当前项,再求剩余项中距离当前项最小距离,循环递归直到list为空,但是遇到问题是,距离当前项的最小距离有相同的项,不是唯一的,例如第三项,第四项距离当前项距离一致且都为最小距离,再循环递归就出现两个分支。