华丽的痘痘
努力成为一个有价值的程序员
登录
注册
全站
当前博客
空间
博客
好友
相册
留言
王亚楠
ID:andycpp
共
45035
次访问,排名
2401
好友
17
人,关注者
31
人
做一名合格地java教师
andycpp的文章
原创 42 篇
翻译 1 篇
转载 13 篇
评论 15 篇
最近评论
zjcone:
很明确,谢谢!
czdvcc:
wow power leveling
hukaibao2007:
不错
wjjdlove:
貌似不错呢
wjjdlove:
thanks
文章分类
JAVA技术
(RSS)
常见算法
(RSS)
随笔
(RSS)
收藏
经典文章
相册
我的相册
高手专栏
Web高手buaawhl的专栏
(RSS)
存档
2008年08月(3)
2007年09月(3)
2007年08月(7)
2007年07月(11)
2007年06月(5)
2007年02月(1)
2006年10月(7)
2006年09月(5)
2006年08月(1)
2006年06月(1)
2006年04月(2)
2006年03月(3)
2006年02月(3)
软件项目交易
订阅我的博客
绘制数字缠绕矩阵
收藏
新一篇: 炒股软件“大智慧”的一些快捷键
|
旧一篇: 在n个数字中,找出所有和为SUM的组合
什么是数字缠绕矩阵呢? 呵呵,这个名字是我起的,好玩而已,举个例子大家就明白了:
01 02 03 04 05
16 17 18 19 06
15 24 25 20 07
14 23 22 21 08
13 12 11 10 09
01 20 19 18 17 16
02 21 32 31 30 15
03 22 33 36 29 14
04 23 34 35 28 13
05 24 25 26 27 12
06 07 08 09 10 11
像这样的矩阵,由阿拉伯数组从外向内缠绕递增生成,就叫做数字缠绕矩阵了。当然,缠绕的方向分为顺时针和逆时针,第一个例子是顺时针,第二个例子是逆时针。
这只是一个比较有趣的小程序,闲来无事写一个玩玩,代码如下:
public
class
Main
...
{
//
测试函数
public
static
void
main(String[] args)
...
{
NumMatrix nm
=
new
NumMatrix();
nm.run(
5
, NumMatrix.DEASIL);
System.out.println();
nm.run(
6
, NumMatrix.ANTICLOCKWISE);
}
}
class
NumMatrix
...
{
public
static
final
int
DEASIL
=
1
;
//
顺时针
public
static
final
int
ANTICLOCKWISE
=
2
;
//
逆时针
/** */
/**
* 绘制出一个数字串缠绕矩阵
*
@param
n 矩阵的尺寸
*
@param
circle 数字串的旋转方向:顺时针或逆时针
*/
public
void
run(
int
n,
int
circle)
...
{
int
[][] a
=
new
int
[n][n];
setNum(a);
setCircle(circle);
while
(
true
)
...
{
if
(move(n, a))
...
{
setNum(a);
turnCount
=
0
;
}
else
...
{
turn();
if
(
++
turnCount
==
2
)
break
;
}
}
for
(
int
i
=
0
; i
<
n; i
++
)
...
{
for
(
int
j
=
0
; j
<
n; j
++
)
...
{
System.out.print(String.format(
"
%1$02d
"
, a[i][j]));
}
System.out.println();
}
reset();
}
//
重置各项参数,为下一次绘制做准备
private
void
reset()
...
{
x
=
0
;
y
=
0
;
num
=
1
;
arrow
=
RIGHT;
turnCount
=
0
;
}
//
移动一格,若成功移动返回true,若撞墙则返回false
private
boolean
move(
int
n,
int
[][] a)
...
{
switch
(arrow)
...
{
case
RIGHT:
if
(y
+
1
==
n
||
a[x][y
+
1
]
>
0
)
return
false
;
y
++
;
break
;
case
DOWN:
if
(x
+
1
==
n
||
a[x
+
1
][y]
>
0
)
return
false
;
x
++
;
break
;
case
LEFT:
if
(y
==
0
||
a[x][y
-
1
]
>
0
)
return
false
;
y
--
;
break
;
case
UP:
if
(x
==
0
||
a[x
-
1
][y]
>
0
)
return
false
;
x
--
;
break
;
}
return
true
;
}
//
将数字填入当前空白格子
private
void
setNum(
int
[][] a)
...
{
a[x][y]
=
num
++
;
}
//
根据旋转方向为顺时针和逆时针,进行拐弯
private
void
turn()
...
{
switch
(circle)
...
{
case
DEASIL:
arrow
=
(arrow
+
1
)
%
4
;
break
;
case
ANTICLOCKWISE:
arrow
=
(arrow
+
3
)
%
4
;
}
}
//
设置旋转方向
private
void
setCircle(
int
circle)
...
{
this
.circle
=
circle;
switch
(circle)
...
{
case
DEASIL:
arrow
=
RIGHT;
break
;
case
ANTICLOCKWISE:
arrow
=
DOWN;
break
;
}
}
private
int
turnCount
=
0
;
private
int
x
=
0
;
private
int
y
=
0
;
private
int
num
=
1
;
private
final
static
int
RIGHT
=
0
;
private
final
static
int
DOWN
=
1
;
private
final
static
int
LEFT
=
2
;
private
final
static
int
UP
=
3
;
private
int
circle;
private
int
arrow;
}
发表于 @
2007年09月23日 12:21:00
|
评论(
loading...
)
|
编辑
新一篇: 炒股软件“大智慧”的一些快捷键
|
旧一篇: 在n个数字中,找出所有和为SUM的组合
评论:没有评论。
发表评论
姓 名:
主 页:
校验码:
看不清,换一张
当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击
登录