标题:结果始终是奇数,要弄成偶数,咋办呢?
取消只看楼主
学习要努力
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-11-26
结帖率:0
已结贴  问题点数:20 回复次数:1 
结果始终是奇数,要弄成偶数,咋办呢?

奇数幻方
Sub test()                           
                             
Dim n As Integer, num As Integer, row, col As Integer, i As Integer, c As Integer                           
                             
Dim M() As Integer                           
                             
n = InputBox("请输入一个奇数:")                           
                             
If n Mod 2 = 0 Then                           
                             
    MsgBox "请输入一个奇数!"                           
                             
    Exit Sub                           
                             
End If                           
                             
ReDim M(n - 1, n - 1)                           
                             
num = n ^ 2                           
                             
row = 0                           
                             
col = (n - 1) \ 2                           
                             
i = 1                           
                             
Do While i <= num                           
                             
    If row = -1 Then row = n - 1                           
                             
    If col = n Then col = 0                           
                             
    M(row, col) = i                           
                             
    If i Mod n = 0 Then                           
                             
        row = row + 1                           
                             
    Else                           
                             
        row = row - 1                           
                             
        col = col + 1                           
                             
    End If                           
                             
    i = i + 1                           
                             
Loop                           
                             
[a1].Value = "输出幻方矩阵 n=" & n                           
                             
For i = 0 To n - 1                           
                             
    For c = 0 To n - 1                           
                             
        Cells(i + 2, c + 1) = M(i, c)                           
                             
    Next c                           
                             
Next i                           
                             
End Sub                           
2013-11-26 10:02
学习要努力
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-11-26
得分:0 
谢谢a814153
2013-11-27 09:52



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-424209-1-1.html




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

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