总觉得做得很麻烦……
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace test3_1
- {
- class Program
- {
- static void right(int[,] a, int t)//右移校正:数组a,a的长度t
- {
- for (int i = t; i > 0; i--)
- for (int j = t; j > 0; j--)
- {
- a[i, j] = a[i - 1, j - 1];
- print(t, a);
- }
- }
- static void print(int i, int[,] a) //打印数组:数组a,a的长度i
- {
- for (int m = 0; m < i; m++)
- {
- for (int n = 0; n < i; n++)
- {
- if (a[n, m] < 10)
- Console.Write(" ");
- Console.Write(a[n, m]+" ");
- }
- Console.WriteLine();
- }
- }
- static void xy(int q,int[,] a,int i)//整理数据:数字q,数组a,a的长度i
- {
- int x=0 , y =0;//数字位置
- for (int z = 1; z < i; z++)
- {
- if (z % 2 == 1)
- {
- for (++x ; x <= z; x++)//向右
- {
- a[x, 0] = q++;
- Console.WriteLine("向右:{0},{1},{2}", a[x, 0], x, 0);
- }
- for (--x ; y < z; y++)//向下
- {
- a[x, y+1 ] = q++;
- Console.WriteLine("向下:{0},{1},{2}", a[x, y+1], x, y+1);
- }
- for (--x; x >= 0; x--)//向左
- {
- a[x, y] = q++;
- Console.WriteLine("向左:{0},{1},{2}", a[x, y], x, y);
- }
- print(i, a);
- }
- else
- {
- right(a, a.GetLength(1) - 1);
- for (++x,++y ; x >= 0; x--)//向左
- {
- a[x, y] = q++;
- Console.WriteLine("向左:{0},{1},{2}", a[x, y], x, y);
- }
- for (++x; y > 0; y--)//向上
- {
- a[x, y-1] = q++;
- Console.WriteLine("向上:{0},{1},{2}", a[x, y-1], x, y-1);
- }
- for (; x < z; x++)//向右
- {
- a[x+1, y] = q++;
- Console.WriteLine("向右:{0},{1},{2}", a[x+1, y], x+1, y);
- }
- print(i, a);
- }
- }
- }
- static void Main(string[] args)
- {
- int i ;
- Console .Write ("请输入i:");
- i=Convert .ToInt16 (Console .ReadLine ());
- int[,] myarray = new int[i, i];//二元数组表示方阵
- myarray[0, 0] = 1;
- int q = 2;
- xy(q, myarray , i);
- Console.ReadKey();
- }
- }
- }