110,536
社区成员
发帖
与我相关
我的任务
分享
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中的数字组成数组,全部找出来。
怎么写程序??
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();
}
string result=string.Empty;
foreach(int[] arr in array)
foreach(int n in arr)
{
if(n>0) result+=n+" ";
}
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);
}
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);
}
}
//优化一下,支持锯齿数组。
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();
}