| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 691 人关注过本帖
标题:n阶魔方阵
只看楼主 加入收藏
guiyangl015
Rank: 1
等 级:新手上路
帖 子:125
专家分:0
注 册:2006-5-21
收藏
 问题点数:0 回复次数:4 
n阶魔方阵

下面是一段产生n阶魔方阵的代码,运行时下界越标,红色部分有问题
但是本人愚钝,实在不知道如何改动?请赐教!!
Private Sub Command1_Click()
Dim k%, i%, j%, m() As Integer
n = Val(Text1.Text)

i = 1
j = n / 2 + 1

ReDim m(1 To n, 1 To n)

For k = 1 To n * n
m(i, j) = k

If (k Mod n = 0) Then
i = i + 1
ElseIf i = 1 Then
i = n: j = j + 1
ElseIf j = n Then
j = 1: i = i - 1
Else
i = i - 1: j = j - 1
End If

Next k

For i = 1 To n
For j = 1 To n
Picture1.Print m(i, j); Tab(j * 5)
Next j
Picture1.Print
Next i

End Sub

搜索更多相关主题的帖子: 魔方 
2006-06-09 08:22
tang688
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:1219
专家分:35
注 册:2004-12-25
收藏
得分:0 
Private Sub Command1_Click()
Dim k%, i%, j%, m() As Integer
n = Val(Text1.Text)

i = 1
j = n / 2 + 1

ReDim m(1 To n, 1 To n)

For k = 1 To n * n
msgbox k&","&i&","&j

加上这一条,看看k是什么在什么条件下越标,然后根据当时的情况找原因吧.
m(i, j) = k
If (k Mod n = 0) Then
i = i + 1
ElseIf i = 1 Then
i = n: j = j + 1
ElseIf j = n Then
j = 1: i = i - 1
Else
i = i - 1: j = j - 1
End If
Next k

For i = 1 To n
For j = 1 To n
Picture1.Print m(i, j); Tab(j * 5)
Next j
Picture1.Print
Next i

End Sub


2006-06-09 10:06
guiyangl015
Rank: 1
等 级:新手上路
帖 子:125
专家分:0
注 册:2006-5-21
收藏
得分:0 
试过了,
说是语法错误,何解??

站的高,才能看的远!!
2006-06-09 23:53
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 

Private Sub Command1_Click()
Dim k%, i%, j%, m() As Integer
n = Val(Text1.Text)
i = 1
j = (n + 1) / 2
ReDim m(1 To n, 1 To n)
m(i, j) = 1
For k = 2 To n * n
If (k - 1) Mod n = 0 Then
i = i + 1
Else
i = IIf(i = 1, n, i - 1)
j = IIf(j = n, 1, j + 1)
End If
m(i, j) = k
Next k

For i = 1 To n
For j = 1 To n
Picture1.Print m(i, j); Tab(j * 5);
Next j
Picture1.Print
Next i
End Sub


我的msn: myfend@
2006-06-10 09:52
Lysuse
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-3-26
收藏
得分:0 

2006-06-10 10:47
快速回复:n阶魔方阵
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025007 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved