sth를 사용해야하는 특정 알고리즘을 구현하려고합니다. 무엇이 4 차원입니까?
하지만 새 목록에 요소를 추가하는 데 문제가 있습니다. 목록에 새 값을 추가 할 때 : temp2, 다른 목록의 이전 값이 변경됩니다.
아무에게도 내가 뭘 잘못 보여줄 수 있습니까?이전 목록의 변경 요소를 목록에 추가하는 것
예 : N 대
= 4, K = 2 :
때 = 3, J = 1
-> 탭 [3,1] = [{1} {2} { 3}] (항상 이렇게 보일 것임)
다음에 i = 3, j = 2 탭 [3,1]이 변경 될 때 :
-> 탭 [3,1] : [{1,3} , [2,3], {3}]
여기서, i = 4, j = 1 일 때
→ tab [4,1] = [{1,3}, {2,3}, {3 }, {4}]
그런 다음 i = 4, j = 2 일 때 탭 [4,1]이 변경 중임 :
-> 탭 [4,1] : [{1,3,4}, {2,3,4} 나는 내 수업에서 알고리즘을 구현하고있어, 그래서 내가 그들을 사용할 필요가 - 나 같은 일반 변수를 사용하는 이유는 프로그램
while (true)
{
int n = int.Parse(Console.ReadLine());
int k = int.Parse(Console.ReadLine());
List<List<int>>[,] tab = new List<List<int>>[n + 1, k + 1];
for (int i = 1; i <= n; i++)
{
int j = 0;
while (j <= i && j <= k)
{
tab[i, j] = new List<List<int>>();
List<int> toAdd = new List<int>();
if (j == 0)
{
toAdd.Clear(); //add empty
tab[i, 0].Add(toAdd);
}
else if (i == j)
{
for (int p = 1; p <= j; p++)
toAdd.Add(p);
tab[i, i].Add(toAdd);
}
else
{
var temp = new List<List<int>>();
var temp2 = new List<List<int>>();
temp.AddRange(tab[i - 1, j]);
temp2.AddRange(tab[i - 1, j - 1]);
foreach (var x in temp2) //add 'i' as last element for each element of temp2
x.Add(i); //here is sth wrong (bad reference?)
temp2.Reverse(); //reverse order
tab[i, j].AddRange(temp);
tab[i, j].AddRange(temp2);
}
j++;
}
}
//show tab[n,k] in console
}
에서 알고리즘의 3,4}, {4}]
내 코드입니다.
그것은 그렇게 간단했다! 고마워요! D btw. 두 번째 줄은 다음과 같아야합니다 :'temp2.AddRange (tab [i-1, j-1] .Select (l => new List (l)))'; - 대괄호가 잘못되었습니다. –
Monic