我必须将递归程序重新动态化以适应大学任务,并且在执行此操作时遇到错误:
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
该错误似乎是在线发生的,max = Max(sum2[l], array[j - 1, i - 1]);
static int F2(int k, int n)
{
int[] sum2 = new int[100];
int max = 0;
int[,] array = new int[n + 1, k + 1];
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= k; j++)
{
//array[i, j] = int.MaxValue;
if (i == 0) array[i, j] += 0;
else if (j == 1) array[i, j] += p[i];
else
{
for (int l = 1; l < n; l++)
{
for (int e = l; e < n; e++)
sum2[l] += p[i];
max = Max(sum2[l], array[j - 1, i - 1]);
array[i, j] = int.MaxValue;
if (max < array[i, j]) array[i, j] = max;
}
}
}
}
return array[k, n];
}
public static int Max(int a, int b) { return a > b ? a : b; }
那么,如何解决这个问题以及我该怎么做呢?