| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 456 人关注过本帖
标题:结果始终是奇数,要弄成偶数,咋办呢?
取消只看楼主 加入收藏
学习要努力
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
快速回复:结果始终是奇数,要弄成偶数,咋办呢?
数据加载中...
 
   



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

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