求助:多for循环
各位大虾好!
遇到了一个多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循环,看别人说递归可以解决,可以做不出来。
请问各位大侠,有没有什么好办法解决,或者递归该怎么做,谢谢了!
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[][] data =
{
new int[] { 1, 2, 3 },
new int[] { 1, 3, 5 },
new int[] { 1, 2, 6 },
new int[] { 3, 7, 8 },
new int[] { 2, 4, 5 },
new int[] { 3, 5, 6 }
};
var result = data.GroupBy(x => x[0]).Select(x => x.First())
.GroupBy(x => x[1]).Select(x => x.First())
.GroupBy(x => x[2]).Select(x => x.First());
foreach (var item in result)
{
Console.WriteLine(string.Join(", ", item.ToArray()));
}
}
}
}