二维数组循环这个怎么写?

hsie168518 2008-11-12 10:20:51
int[][] array = new int[9][];

array[0] = new int[] { 3, 0, 5, 6, 0, 0, 0, 8, 0 };
array[1] = new int[] { 0, 0, 8, 0, 0, 0, 6, 0, 0 };
array[2] = new int[] { 0, 7, 0, 0, 0, 1, 0, 5, 0 };

array[3] = new int[] { 9, 6, 0, 0, 2, 0, 0, 0, 0 };
array[4] = new int[] { 0, 0, 0, 9, 0, 5, 0, 0, 0 };
array[5] = new int[] { 0, 0, 0, 0, 4, 0, 0, 3, 9 };

array[6] = new int[] { 0, 4, 0, 5, 0, 0, 0, 7, 0 };
array[7] = new int[] { 0, 0, 7, 0, 0, 0, 2, 0, 0 };
array[8] = new int[] { 0, 1, 0, 0, 0, 7, 3, 0, 4 };

我定义了如上的二维数组。我要找到这些数。

int[] a01=new int[]{2,9};
int[] a04=new int[]{1,7,9};
..
int[] a10=new int[]{1,2,4,5,7};
..
即列行循环,若值为0,就以座标形式定义变量,找出横竖不在1到9中的数字组成数组,全部找出来。

怎么写程序??
...全文
311 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
止戈而立 2008-11-12
  • 打赏
  • 举报
回复
		private void button1_Click(object sender, System.EventArgs e)
{
string result=string.Empty;
int[][] array = new int[9][];

array[0] = new int[] { 3, 0, 5, 6, 0, 0, 0, 8, 0 };
array[1] = new int[] { 0, 0, 8, 0, 0, 0, 6, 0, 0 };
array[2] = new int[] { 0, 7, 0, 0, 0, 1, 0, 5, 0 };

array[3] = new int[] { 9, 6, 0, 0, 2, 0, 0, 0, 0 };
array[4] = new int[] { 0, 0, 0, 9, 0, 5, 0, 0, 0 };
array[5] = new int[] { 0, 0, 0, 0, 4, 0, 0, 3, 9 };

array[6] = new int[] { 0, 4, 0, 5, 0, 0, 0, 7, 0 };
array[7] = new int[] { 0, 0, 7, 0, 0, 0, 2, 0, 0 };
array[8] = new int[] { 0, 1, 0, 0, 0, 7, 3, 0, 4 };
for(int i=0;i<array.Length;i++)
{
for(int j=0;j<array[i].Length;j++)
{
if(array[i][j]==0) result+=GetResult(i,j)+"\r\n";
}
}
this.textBox2.Text=result;

}

private string GetResult(int row,int colume)
{
ArrayList list=new ArrayList(new int[]{1,2,3,4,5,6,7,8,9});
string result=string.Empty;
int[][] array = new int[9][];

array[0] = new int[] { 3, 0, 5, 6, 0, 0, 0, 8, 0 };
array[1] = new int[] { 0, 0, 8, 0, 0, 0, 6, 0, 0 };
array[2] = new int[] { 0, 7, 0, 0, 0, 1, 0, 5, 0 };

array[3] = new int[] { 9, 6, 0, 0, 2, 0, 0, 0, 0 };
array[4] = new int[] { 0, 0, 0, 9, 0, 5, 0, 0, 0 };
array[5] = new int[] { 0, 0, 0, 0, 4, 0, 0, 3, 9 };

array[6] = new int[] { 0, 4, 0, 5, 0, 0, 0, 7, 0 };
array[7] = new int[] { 0, 0, 7, 0, 0, 0, 2, 0, 0 };
array[8] = new int[] { 0, 1, 0, 0, 0, 7, 3, 0, 4 };
for(int i=0;i<array[row].Length;i++)
{
int temp=array[row][i];
if(temp>0&&list.Contains(temp)) list.Remove(temp);
}
for(int i=0;i<array.Length;i++)
{
int temp=array[i][colume];
if(temp>0&&list.Contains(temp)) list.Remove(temp);
}
foreach(int n in list)
{
result+=n+" ";
}
return "a"+row+colume+"="+result.Trim();
}

运行结果:
a01=2 9
a04=1 7 9
a05=2 4 9
a06=1 4 7 9
a08=1 2 7
a10=1 2 4 5 7
a11=2 3 5 9
a13=1 2 3 4 7
a14=1 3 5 7 9
a15=2 3 4 9
a17=1 2 4 9
a18=1 2 3 5 7
a20=2 4 6 8
a22=2 3 4 6 9
a23=2 3 4 8
a24=3 6 8 9
a26=4 8 9
a28=2 3 6 8
a32=1 3 4
a33=1 3 4 7 8
a35=3 4 8
a36=1 4 5 7 8
a37=1 4
a38=1 3 5 7 8
a40=1 2 4 6 7 8
a41=2 3 8
a42=1 2 3 4 6
a44=1 3 6 7 8
a46=1 4 7 8
a47=1 2 4 6
a48=1 2 3 6 7 8
a50=1 2 5 6 7 8
a51=2 5 8
a52=1 2 6
a53=1 2 7 8
a55=2 6 8
a56=1 5 7 8
a60=1 2 6 8
a62=1 2 3 6 9
a64=1 3 6 8 9
a65=2 3 6 8 9
a66=1 8 9
a68=1 2 3 6 8
a70=1 4 5 6 8
a71=3 5 8 9
a73=1 3 4 8
a74=1 3 5 6 8 9
a75=3 4 6 8 9
a77=1 4 6 9
a78=1 3 5 6 8
a80=2 5 6 8
a82=2 6 9
a83=2 8
a84=5 6 8 9
a87=2 6 9
止戈而立 2008-11-12
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 hsie168518 的回复:]
引用 4 楼 min_jie 的回复:
C# code
string result=string.Empty;
foreach(int[] arr in array)
foreach(int n in arr)
{
if(n>0) result+=n+" ";
}


这是把所有的非0取出来,我不是要这个
[/Quote]

我只是想跟你说可以这样子循环。。其实你要的结果实现起来也不难。。
hsie168518 2008-11-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 min_jie 的回复:]
C# code
string result=string.Empty;
foreach(int[] arr in array)
foreach(int n in arr)
{
if(n>0) result+=n+" ";
}
[/Quote]

这是把所有的非0取出来,我不是要这个
bbbbbb888888 2008-11-12
  • 打赏
  • 举报
回复
4L正解.
hsie168518 2008-11-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 amandag 的回复:]
没看懂题目
[/Quote]

先从第一行循环,发现array[0][1]=0,

这里行有 (3568)竖有(7641),在1到9中只是(2,9)没包涵在其中。

所以找到a01="2,9";
lovehongyun 2008-11-12
  • 打赏
  • 举报
回复
?
amandag 2008-11-12
  • 打赏
  • 举报
回复
没看懂题目
止戈而立 2008-11-12
  • 打赏
  • 举报
回复

string result=string.Empty;
foreach(int[] arr in array)
foreach(int n in arr)
{
if(n>0) result+=n+" ";
}
hsie168518 2008-11-12
  • 打赏
  • 举报
回复


不知道怎么写for循环,help
jiezi316 2008-11-12
  • 打赏
  • 举报
回复
你这个好象是嵌套数组又叫锯齿数组,好象和多维数组有点区别
swalp 2008-11-12
  • 打赏
  • 举报
回复
学习来的
chinaicm 2008-11-12
  • 打赏
  • 举报
回复
public class ArrayInfo
{
int[][] array = new int[9][];
public ArrayInfo()
{
array[0] = new int[] { 3, 0, 5, 6, 0, 0, 0, 8, 0 };
array[1] = new int[] { 0, 0, 8, 0, 0, 0, 6, 0, 0 };
array[2] = new int[] { 0, 7, 0, 0, 0, 1, 0, 5, 0 };
array[3] = new int[] { 9, 6, 0, 0, 2, 0, 0, 0, 0 };
array[4] = new int[] { 0, 0, 0, 9, 0, 5, 0, 0, 0 };
array[5] = new int[] { 0, 0, 0, 0, 4, 0, 0, 3, 9 };
array[6] = new int[] { 0, 4, 0, 5, 0, 0, 0, 7, 0 };
array[7] = new int[] { 0, 0, 7, 0, 0, 0, 2, 0, 0 };
array[8] = new int[] { 0, 1, 0, 0, 0, 7, 3, 0, 4 };
}

public Dictionary<string, int[]> Get()
{
Dictionary<string, int[]> list = new Dictionary<string, int[]>();
for (int i = 0; i < array.Length; i++)
{
for (int j = 0; j < array[i].Length; j++)
{
if (array[i][j] == 0)
{
string position = "a" + i.ToString() + j.ToString();
int[] a = GetNotInArray(array[i], GetColumn(j));
list.Add(position,a);
}
}
}
return list;
}

public int[] GetColumn(int column)
{
ArrayList list = new ArrayList();
for (int i = 0; i < array.Length; i++)
{
list.Add(array[i][column]);
}
return (int[])list.ToArray(typeof(int));
}

public int[] GetNotInArray(int[] A, int[] B)
{
ArrayList returnList = new ArrayList();
ArrayList list = new ArrayList();

for(int i =0;i<A.Length;i++)
{
if (A[i] != 0 && !list.Contains(A[i]))
{
list.Add(A[i]);
}
}

for(int j =0;j<B.Length;j++)
{
if (B[j] != 0 && !list.Contains(B[j]))
{
list.Add(B[j]);
}
}

for (int k = 1; k <= 9; k++)
{
if (!list.Contains(k))
{
returnList.Add(k);
}
}

return (int[])returnList.ToArray(typeof(int));
}
}
claymore1114 2008-11-12
  • 打赏
  • 举报
回复
楼主的数组 并不是二维数组,是锯齿数组。二维数组 是[,]这样。
你的数组好麻烦。
建议用二维数组,再利用2个 for循环,就能得出每一个值。
hsie168518 2008-11-12
  • 打赏
  • 举报
回复


好怪,就少了一个刮号??

太感谢你了,接分
止戈而立 2008-11-12
  • 打赏
  • 举报
回复
ArrayList list1 = new ArrayList(new int[] { });

for (int i = 0; i < array.Length; i++)
{
for (int j = 0; j < array[i].Length; j++)
{
if (array[i][j] == 0)
{
result = GetResult(i, j, array);
list1.Add(result);
}

}
}

知道你的意思了。。这样就不会81行了。。
hsie168518 2008-11-12
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 min_jie 的回复:]
从a00到a88,肯定是81啦。。[/Quote]

把重复的数据删除怎么删?为什么我的删不干净?

 list1.Remove("");            
for(int m=0;m<list1.Count;m++)
for(int n=m+1;n<list1.Count;n++)
if(list1[n]==list1[m])
{
string tmp=list1[n].ToString();
list1.Remove(tmp);
}
chengdishuai 2008-11-12
  • 打赏
  • 举报
回复
private void button1_Click(object sender, System.EventArgs e)
{
string result=string.Empty;
int[][] array = new int[9][];

array[0] = new int[] { 3, 0, 5, 6, 0, 0, 0, 8, 0 };
array[1] = new int[] { 0, 0, 8, 0, 0, 0, 6, 0, 0 };
array[2] = new int[] { 0, 7, 0, 0, 0, 1, 0, 5, 0 };

array[3] = new int[] { 9, 6, 0, 0, 2, 0, 0, 0, 0 };
array[4] = new int[] { 0, 0, 0, 9, 0, 5, 0, 0, 0 };
array[5] = new int[] { 0, 0, 0, 0, 4, 0, 0, 3, 9 };

array[6] = new int[] { 0, 4, 0, 5, 0, 0, 0, 7, 0 };
array[7] = new int[] { 0, 0, 7, 0, 0, 0, 2, 0, 0 };
array[8] = new int[] { 0, 1, 0, 0, 0, 7, 3, 0, 4 };
for(int i=0;i<array.Length;i++)
{
for(int j=0;j<array[i].Length;j++)
{
if(array[i][j]==0) result+=GetResult(i,j)+"\r\n";
}
}
this.textBox2.Text=result;

}

private string GetResult(int row,int colume)
{
ArrayList list=new ArrayList(new int[]{1,2,3,4,5,6,7,8,9});
string result=string.Empty;
int[][] array = new int[9][];

array[0] = new int[] { 3, 0, 5, 6, 0, 0, 0, 8, 0 };
array[1] = new int[] { 0, 0, 8, 0, 0, 0, 6, 0, 0 };
array[2] = new int[] { 0, 7, 0, 0, 0, 1, 0, 5, 0 };

array[3] = new int[] { 9, 6, 0, 0, 2, 0, 0, 0, 0 };
array[4] = new int[] { 0, 0, 0, 9, 0, 5, 0, 0, 0 };
array[5] = new int[] { 0, 0, 0, 0, 4, 0, 0, 3, 9 };

array[6] = new int[] { 0, 4, 0, 5, 0, 0, 0, 7, 0 };
array[7] = new int[] { 0, 0, 7, 0, 0, 0, 2, 0, 0 };
array[8] = new int[] { 0, 1, 0, 0, 0, 7, 3, 0, 4 };
for(int i=0;i<array[row].Length;i++)
{
int temp=array[row][i];
if(temp>0&&list.Contains(temp)) list.Remove(temp);
}
for(int i=0;i<array.Length;i++)
{
int temp=array[i][colume];
if(temp>0&&list.Contains(temp)) list.Remove(temp);
}
foreach(int n in list)
{
result+=n+" ";
}
return "a"+row+colume+"="+result.Trim();
}
止戈而立 2008-11-12
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 hsie168518 的回复:]



C# code ArrayList list1 = new ArrayList(new int[] { });

for (int i = 0; i < array.Length; i++)
{
for (int j = 0; j < array[i].Length; j++)
{
if (array[i][j] == 0) result = GetResult(i, j, array);
list1.Add(result);
}
}



为什么有81行数据?

[/Quote]
从a00到a88,肯定是81啦。。
hsie168518 2008-11-12
  • 打赏
  • 举报
回复


 ArrayList list1 = new ArrayList(new int[] { });

for (int i = 0; i < array.Length; i++)
{
for (int j = 0; j < array[i].Length; j++)
{
if (array[i][j] == 0) result = GetResult(i, j, array);
list1.Add(result);
}
}


为什么有81行数据?
止戈而立 2008-11-12
  • 打赏
  • 举报
回复
//优化一下,支持锯齿数组。
private void button1_Click(object sender, System.EventArgs e)
{
string result=string.Empty;
int[][] array = new int[9][];

array[0] = new int[] { 3, 0, 5, 6, 0, 0, 0, 8, 0 };
array[1] = new int[] { 0, 0, 8, 0, 0, 0, 6, 0, 0 };
array[2] = new int[] { 0, 7, 0, 0, 0, 1, 0, 5, 0 };

array[3] = new int[] { 9, 6, 0, 0, 2, 0, 0, 0, 0 };
array[4] = new int[] { 0, 0, 0, 9, 0, 5, 0, 0, 0 };
array[5] = new int[] { 0, 0, 0, 0, 4, 0, 0, 3, 9 };

array[6] = new int[] { 0, 4, 0, 5, 0, 0, 0, 7, 0 };
array[7] = new int[] { 0, 0, 7, 0, 0, 0, 2, 0, 0 };
array[8] = new int[] { 0, 1, 0, 0, 0, 7, 3, 0, 4 };
for(int i=0;i<array.Length;i++)
{
for(int j=0;j<array[i].Length;j++)
{
if(array[i][j]==0) result+=GetResult(i,j,array)+"\r\n";
}
}
this.textBox2.Text=result;

}

private string GetResult(int row,int colume,int[][] array)
{
ArrayList list=new ArrayList(new int[]{1,2,3,4,5,6,7,8,9});
string result=string.Empty;
for(int i=0;i<array[row].Length;i++)
{
int temp=array[row][i];
if(temp>0&&list.Contains(temp)) list.Remove(temp);
}
for(int i=0;i<array.Length;i++)
{
if(array[i].Length<=colume) continue;
int temp=array[i][colume];
if(temp>0&&list.Contains(temp)) list.Remove(temp);
}
foreach(int n in list)
{
result+=n+" ";
}
return "a"+row+colume+"="+result.Trim();
}
加载更多回复(1)

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧