| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 456 人关注过本帖
标题:结果始终是奇数,要弄成偶数,咋办呢?
只看楼主 加入收藏
学习要努力
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-11-26
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
结果始终是奇数,要弄成偶数,咋办呢?

奇数幻方
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
Artless
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:4211
专家分:28888
注 册:2009-4-8
收藏
得分:7 
vb6.0?

无知
2013-11-26 12:20
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:7 
找公式。

这个有公式填充数据的。百度一下就是了。

授人于鱼,不如授人于渔
早已停用QQ了
2013-11-26 17:26
a814153
Rank: 2
等 级:论坛游民
威 望:1
帖 子:13
专家分:24
注 册:2011-6-9
收藏
得分:7 
http://extjs2.
2013-11-26 19:18
学习要努力
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-11-26
收藏
得分:0 
谢谢a814153
2013-11-27 09:52
快速回复:结果始终是奇数,要弄成偶数,咋办呢?
数据加载中...
 
   



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

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