|
-
February 17th, 2012, 11:05 AM
#6
Re: Writing a complex loop
thanks for your reply.
no, i'm not trying to print the numbers on the screen. as i mentioned in my code, it's just a list box to show me the progress so i can realize the result is what i want to get or not.
let's make it clear...
i have an array with different values. let's call it "MyMainArray".
now i would like to get a combination of MyMainArray values to reach my goal which is a number (say 63879). ofcourse this combination should be made by a specific number of elements. (say 20 elements of MyMainArray)
so i declared another array which represents an index. let's call this one "MyArray"
in my case, i have to start from the biggest value (MyMainArray.Length - 1). so i filled the MyArray values with this number.
now i have to loop through all combinations to see which one is equivalent to my goal.
so i have to loop through all indexes one by one and put these indexes in MyMainArray and get the sum of these values.
anyway, i worked on it and modified the code like this one, but it doesn't work properly:
Code:
int iCurrentRow = 0;
int iTempRow = 0;
do
{
if (aiPlanes[iCurrentRow] == 0)
{
iCurrentRow++;
}
for (int j = iCurrentRow - 1; j >= 0; j--)
{
aiPlanes[j] = i - 1;
}
aiPlanes[iCurrentRow]--;
iTempRow = 0;
do
{
if (aiPlanes[iTempRow] == 0)
{
iTempRow++;
if (iTempRow >= iCurrentRow) { MessageBox.Show(iTempRow.ToString()); break; }
}
for (int j = iTempRow - 1; j >= 0; j--)
{
aiPlanes[j] = i - 1;
}
if (iTempRow < iCurrentRow) aiPlanes[iTempRow]--;
for (int j = 0; j <= iTempRow; j++)
{
for (int k = i - 1; k >= 0; k--)
{
aiPlanes[j] = k;
string bobo = "";
for (int b = 0; b < iHalfPlanes; b++)
{
bobo += aiPlanes[b].ToString() + ' ';
}
lstResult.Items.Add(bobo);
lstResult.SelectedIndex = lstResult.Items.Count - 1;
}
}
} while (iTempRow <= iCurrentRow);
} while (iCurrentRow <= iHalfPlanes);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|