解决方案 »
- 移动控件闪烁问题
- 我建了一个.Net开发的QQ群,欢迎各位同仁踊跃加入!
- 在WinForm程序中如何动态获得Label标签并给它赋值?
- 在程序中怎么让程序停留几秒再去执行后面的程序???
- winform中验证email或http的正则表达式是怎么写的
- 问一个问题,xsd在什么情况比必须用?我做了2年asp.net web 程序都没用过,还有xml,我也用不上啊?怎么回事?
- 打印紧急?!!!!
- 求问,求问关于文件缓存的问题。。
- 如何在Oracle里使用通配符,Like?
- vs2010编译问题 重新生成正常,但写的代码,在调试模式和单独运行模式中不执行新写的代码
- 用SQL语句写一个触发器
- 求一个分页查询SQL语句
static void Main(string[] args)
{
PrintBox(5);
} static void PrintBox(int row)
{
int[,] array = new int[row, row];
int index = 1;
int j = 0, k = 0; #region first
int right = row - 1, left = 0, bottom = row - 1, top = 1;
for (int i = 0; i < row / 2 + row % 2; i++)
{
while (k <= right)
array[k++, j] = index++;
k--;
j++;
while (j <= bottom)
array[k, j++] = index++;
j--;
k--;
while (k >= left)
array[k--, j] = index++;
k++;
j--;
while (j >= top)
array[k, j--] = index++;
j++;
k++;
right--;
left++;
bottom--;
top++;
}
for (int a = 0; a < row; a++)
{
for (int b = 0; b < row; b++)
Console.Write(array[a, b].ToString().PadLeft(2, '0') + " ");
Console.WriteLine();
}
Console.WriteLine("-----");
#endregion }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;namespace ConsoleApplication1
{
public enum Direction
{
Down,
Right,
Up,
Left
}
class ScrewSqure
{
//根据当前的方向计算下一个方向
private static Direction ChangeDirection(Direction dir)
{
int d = (int)dir;
return (Direction)((++d) % 4);
}
private static bool PosIsValid(int[,] data, Point p)
{
int n = data.GetLength(0);
if (p.X < 0 || p.X >= n) return false;
if (p.Y < 0 || p.Y >= n) return false;
if (data[p.X, p.Y] != 0) return false;
return true;
}
private static void GetNextPos(int[,] data, ref Point p, ref Direction dir)
{
int n = data.GetLength(0);
Point next = new Point();
for (; ; )
{
next.X = p.X;
next.Y = p.Y;
switch (dir)
{
case Direction.Down:
next.X++;
break;
case Direction.Right:
next.Y++;
break;
case Direction.Up:
next.X--;
break;
case Direction.Left:
next.Y--;
break;
default:
break;
}
if (!PosIsValid(data, next))
{
dir = ChangeDirection(dir);
}
else break;
}
p.X = next.X;
p.Y = next.Y;
}
private static void FillMatrics(int[,] data, Direction dir)
{
Point p = new Point();
p.X = p.Y = 0;
int n = data.GetLength(0);
for (int i = 1; i <= n*n; i++)
{
data[p.X, p.Y] = i;
if (i < n * n)
{
GetNextPos(data, ref p, ref dir);
}
}
}
static void Main(string[] args)
{
int[,] ar=new int[10,10];
FillMatrics (ar,Direction.Down );
for (int i = 0; i <ar.GetLength(0); i++)
{
for (int j = 0; j < ar.GetLength (1); j++)
{
Console.Write("{0,4}", ar[i, j]);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
搞点。。
{
static int[,] Fill(int n)
{
int d = 0;//↓, →, ↑, ←
int[] di = { 1, 0, -1, 0 };
int[] dj = { 0, 1, 0, -1 };
int[,] a = new int[n, n];
for (int i = 0, j = 0, k = 1; k <= n * n; i += di[d], j += dj[d], k++)
{
a[i, j] = k;
if (i + di[d] < 0 || i + di[d] >= n || j + dj[d] < 0 || j + dj[d] >= n || a[i + di[d], j + dj[d]] > 0) d = (d + 1) % 4;
}
return a;
} static void Main()
{
int[,] a = Fill(5);
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
Console.Write("{0,3}", a[i, j]);
Console.WriteLine();
}
}
}
/* 程序输出:
1 16 15 14 13
2 17 24 23 12
3 18 25 22 11
4 19 20 21 10
5 6 7 8 9
*/