Fortran Coder

查看: 831|回复: 2
打印 上一主题 下一主题

[数值问题] Fortran幻方

[复制链接]

2

帖子

1

主题

0

精华

新人

F 币
11 元
贡献
5 点
跳转到指定楼层
楼主
发表于 2023-12-2 01:17:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1F 币
题目:利用幻方错位补角写法封装子程序,令其可以给出任意奇阶幻方。
有大佬可以解答一下么,实在是写不出来了。

最佳答案

查看完整内容

[mw_shl_code=fortran,true]program main implicit none call magic_square(3) call magic_square(5) call magic_square(7) contains subroutine magic_square(n) integer,intent(in)::n integer::a(n,n) integer::i write(*,"(A,g0)")"n=",n a=reshape([(i,i=1,n*n)],shape=[n,n]) a=cshift(a,[(i,i=-n/2,n/2)],dim=1) a=cshift(a,[(i,i=-n/2,n/2)] ...
分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

159

帖子

2

主题

1

精华

大师

Vim

F 币
961 元
贡献
469 点

规矩勋章

沙发
发表于 2023-12-2 01:17:46 | 只看该作者
本帖最后由 Transpose 于 2023-12-2 09:36 编辑

[Fortran] 纯文本查看 复制代码
program main
    implicit none
    call magic_square(3)
    call magic_square(5)
    call magic_square(7)
contains
    subroutine magic_square(n)
        integer,intent(in)::n
        integer::a(n,n)
        integer::i
        write(*,"(A,g0)")"n=",n
        a=reshape([(i,i=1,n*n)],shape=[n,n])
        a=cshift(a,[(i,i=-n/2,n/2)],dim=1)
        a=cshift(a,[(i,i=-n/2,n/2)],dim=2)
        do i=1,n
            write(*,"(*(I3))")a(:,i)
        end do
    end subroutine magic_square
end program main

输出

[Fortran] 纯文本查看 复制代码
n=3
  8  1  6
  3  5  7
  4  9  2
n=5
 17 24  1  8 15
 23  5  7 14 16
  4  6 13 20 22
 10 12 19 21  3
 11 18 25  2  9
n=7
 30 39 48  1 10 19 28
 38 47  7  9 18 27 29
 46  6  8 17 26 35 37
  5 14 16 25 34 36 45
 13 15 24 33 42 44  4
 21 23 32 41 43  3 12
 22 31 40 49  2 11 20



参考错位补角法https://baike.baidu.com/item/%E5%B9%BB%E6%96%B9/169544
回复

使用道具 举报

2

帖子

1

主题

0

精华

新人

F 币
11 元
贡献
5 点
板凳
 楼主| 发表于 2023-12-2 15:02:21 | 只看该作者
Transpose 发表于 2023-12-2 09:34
[mw_shl_code=fortran,true]program main
    implicit none
    call magic_square(3)

万分感谢!!!!!太厉害了!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-4-28 22:07

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表