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

推荐订阅源

WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Visual Studio Blog
宝玉的分享
宝玉的分享
IT之家
IT之家
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
I
InfoQ
B
Blog RSS Feed
T
Threatpost
博客园_首页
M
MIT News - Artificial intelligence
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Know Your Adversary
Know Your Adversary
U
Unit 42
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
月光博客
月光博客
Scott Helme
Scott Helme
T
Tor Project blog
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
S
Schneier on Security
Vercel News
Vercel News
博客园 - Franky
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
L
LangChain Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
The GitHub Blog
The GitHub Blog
雷峰网
雷峰网
Latest news
Latest news
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
S
Security Affairs
A
About on SuperTechFans
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
大猫的无限游戏
大猫的无限游戏
W
WeLiveSecurity
Cisco Talos Blog
Cisco Talos Blog
罗磊的独立博客

博客园 - 左右间

整数划分问题之寻找 一组不大于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;