永发信息网

VB6.0标准下EXE输出魔方阵

答案:2  悬赏:30  手机版
解决时间 2021-07-31 02:50
  • 提问者网友:相思似海深
  • 2021-07-30 02:43

输出魔方阵,魔方阵是由自然数1-n²(n位奇数)构成的方阵,其各行各列以及对角线元素之和均相等

望高手直接给出源代码

最佳答案
  • 五星知识达人网友:归鹤鸣
  • 2021-07-30 03:07

用VB在标准EXE下给你做好了, ,点一次按钮显示一个魔方矩阵


代码如下


Dim n As Long
Function MagicMatrix() As String
Dim S() As Long
Dim x As Long, y As Long


Dim i As Long


ReDim S(1 To n, 1 To n) As Long
x = (n + 1) / 2
y = 1
S(x, y) = 1
For i = 2 To n ^ 2
If x = n And y = 1 Then
y = y + 1
ElseIf x = n Then
x = 1
y = y - 1
ElseIf y = 1 Then
x = x + 1
y = n
ElseIf S(x + 1, y - 1) <> 0 Then
y = y + 1
Else
x = x + 1
y = y - 1
End If
S(x, y) = i
Next
For i = 1 To n
For j = 1 To n
MagicMatrix = MagicMatrix & S(j, i) & " "


Next
MagicMatrix = MagicMatrix & vbCrLf
Next


End Function



Private Sub Command1_Click()
Dim p As Integer
p = 1
Do While (p = 1)
n = InputBox("Please input 奇数n")
If ((n <> 0) And (n <= 15) And (n Mod 2 <> 0)) Then

p = 0
Else
MsgBox "输入有误,请重新输入"
End If
Loop
Text1.Text = MagicMatrix
End Sub


如果你想要生成的exe可执行文件,上QQ我传给你吧,我的QQ:466698654

全部回答
  • 1楼网友:雾月
  • 2021-07-30 03:26
奇数阶幻方: #include <stdio.h> #define N 7 int main() { int a[N][N] = { 0 }; int i, j, x, y, m, n; a[x=0][y=N/2] = 1; for ( i = 2; i <= N*N; ++i ) { m = x-1; n = y+1; if ( x == 0 ) m = N-1; if ( y == N-1 ) n = 0; if ( a[m][n] != 0 ) m = x+1, n = y; a[x=m][y=n] = i; } for ( i = 0; i < N; ++i ) { for ( j = 0; j < N; ++j ) { printf( "%-2d ", a[i][j] ); } putchar( '\n' ); } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯