各位大虾好!遇到了一个多for循环:
程序的目的是查找不同的内容:比如:有下列数据
1 2 3
1 3 5
1 2 6
3 7 8
2 4 5
3 5 6要找出 n 列这些数据中所有数据都不同的数据列
比如:如果 n=3
那么:
1 2 3
3 7 8
2 4 5
符合要求结果,我用for 循环遍历整个数据,从第一个开始,找第二个,有相同项,放弃,无相同项,然后再找第三个。这样程序可行,但是找三组用3个for循环,如果找20~30个,就要用20~30个for循环,看别人说递归可以解决,可以做不出来。请问各位大侠,有没有什么好办法解决,或者递归该怎么做,谢谢了!(我是用VB.net做的,各位随意)
for i=0 to row-1
tt=objcus(i).sen
for j=0 to row-1
yy=objcus(j).sen
if yy=tt then
else
for k=0 to row-1
nn=objcus(k).sen
if tt=nn or yy=nn then
else
debug.print("rowindex=" & rowindex)
end if
next
next
next
程序的目的是查找不同的内容:比如:有下列数据
1 2 3
1 3 5
1 2 6
3 7 8
2 4 5
3 5 6要找出 n 列这些数据中所有数据都不同的数据列
比如:如果 n=3
那么:
1 2 3
3 7 8
2 4 5
符合要求结果,我用for 循环遍历整个数据,从第一个开始,找第二个,有相同项,放弃,无相同项,然后再找第三个。这样程序可行,但是找三组用3个for循环,如果找20~30个,就要用20~30个for循环,看别人说递归可以解决,可以做不出来。请问各位大侠,有没有什么好办法解决,或者递归该怎么做,谢谢了!(我是用VB.net做的,各位随意)
for i=0 to row-1
tt=objcus(i).sen
for j=0 to row-1
yy=objcus(j).sen
if yy=tt then
else
for k=0 to row-1
nn=objcus(k).sen
if tt=nn or yy=nn then
else
debug.print("rowindex=" & rowindex)
end if
next
next
next
private void button1_Click(object sender, EventArgs e)
{
int count = 0;
textBox1.Text = "";
for (int j = 0; j < table.Rows.Count; j++)
{
List<string> strlist = new List<string>();
strlist.Add(table.Rows[j][0].ToString());
strlist.Add(table.Rows[j][1].ToString());
strlist.Add(table.Rows[j][2].ToString());
int str_coun = strlist.Count;
if (str_coun == strlist.Distinct().Count<string>())
{
textBox1.Text += table.Rows[j][0].ToString() + "-" + table.Rows[j][1].ToString() + "-" + table.Rows[j][2].ToString() + "\r\n";
count++;
}
if (count == int.Parse(textBox2.Text))
{
return;
}
}
}
textBox1是输出结果
textBox2相当于你的 n我可以手动输入
int colCount=3,rowCount=8;
int[,] datas = new int[colCount,rowCount];
//....datas初始化略
List<int[]> list = new List<int[]>();
bool find = false;
for (int i = 1; i < rowCount; i++)
{
find = false;
foreach (int[] addrow in list)
{
for (int index = 0; i < colCount; i++)
{
if (addrow[index] == datas[i, index])
{
find = true;
break;
}
}
if (find)
break;
}
if (!find)
{
int[] row = new int[colCount];
for (int k = 0; k < colCount; k++)
{
row[k] = datas[i, k];
}
list.Add(row);
}
}
lyble
先谢谢了!
看得不是很懂,调试的时候出现在
row[k]=datas[i,k]
索引超出了数组界限不好意思,能在关键地方注释两句吗!我看的不是很懂!
for (int i = 1; i < rowCount; i++) 改成
for (int i = 0; i < rowCount; i++) 试试