惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
V
Visual Studio Blog
P
Privacy International News Feed
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
T
Threatpost
宝玉的分享
宝玉的分享
The Last Watchdog
The Last Watchdog
小众软件
小众软件
L
LINUX DO - 最新话题
C
Cisco Blogs
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
罗磊的独立博客
V
V2EX
博客园 - Franky
P
Proofpoint News Feed
SecWiki News
SecWiki News
腾讯CDC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
PCI Perspectives
PCI Perspectives
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
Cisco Talos Blog
Cisco Talos Blog
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题

博客园 - 左右间

整数划分问题之寻找 一组不大于M的互异的整数集,使之和等于N。找出可能的整数集的个数。 关于在cmd命令里的路径包含空格的问题 MSE-项目管理知识体系(一) 导论 导入导出EXCEL数据时有关时间的处理 比较简单的导入导出EXCEL数据的方法 使用aspnet_regiis.exe加密web.config文件 如何使用CAML 批量更新SharePoint List Stsadm 详细文档 关于SPList的Update及AllowUnsafeUpdates 关于silverlight文件的结构 关于VS.NET权限不够的问题 关于Web Part中的Tokens. 关于AD搜索的The server is not operational错误 IIS 中 Service Unavailable问题的解决方法 Flex样式控制小记 如何在WebPart的菜单中添加自定义的Verbs. 关于FireFox网址收藏夹的比较酷的应用 SharePoint中传递Search参数的Url的一些研究 关于使用JavaScript触发ASP.NET Validator验证的问题
幻方阵
左右间 · 2009-01-16 · via 博客园 - 左右间

幻方阵是一个矩阵,即将n*n(n>=3)个数字放入n*n的方格内,使方格的各行、各列及对角线上各数字之各相等。

Merzirac 算法。
在第最上面一行居中的方格内放1,依次向右上方填入2、3、4…,如果右上方已有数字,则向下移一格继续填写。如果坐标值超出上限,则归0,如果坐标值超出下限,则变为最大坐标值。

实现:
public static int[,] Generat(int n)
        {
            int[,] matrix = new int[n, n];

            int x = n / 2;
            int y = n-1;
            int maxNumber = n*n;
            for (int i = 1; i <= maxNumber; i++)
            {
                //修正坐标
                x = GetRealPositon(x, n);
                y = GetRealPositon(y, n);

                if (matrix[x, y] != 0)
                {
                    //到原始方格的正下方方格
                    x--;
                    y = y-2;
                    //修正坐标
                    x = GetRealPositon(x, n);
                    y = GetRealPositon(y, n);

                    //寻找原始方格的正下方中未被附值的方格
                    while (matrix[x, y] != 0)
                    {
                        y--;
                        y = GetRealPositon(y, n);
                    }
                }

                matrix[x, y] = i;

                //到右上角
                x++;
                y++;
            }

            return matrix;
        }

        private static int GetRealPositon(int x, int n)
        {
            if (x < 0)
                return n - 1;
            else if (x >= n)
                return 0;
            else
                return x;
        }

直接的数学公式。

N(I,J)=((I+J+(n-3)/2) MOD n)*n+(I-J+(3*n-1)/2) MOD n +1;