N(奇数)阶幻方解法

2024-03-30 00:38
文章标签 奇数 解法 幻方

本文主要是介绍N(奇数)阶幻方解法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

        幻方也加魔方,通俗点就是N*N的方格中,填入1~n^2个数,使得横坚斜的和都相同。大家最熟悉的应该就是九宫格的3阶了。这儿我只和大家分享一下奇数阶的;因为奇数阶的就只有一个规律,偶数阶的稍微有点复杂(其实我只会4阶,还是从射雕英雄传里面学来的,在写这篇博客的时候我也特地去百度了一下,发现偶数阶的好像不同的阶数规律不一样,所以这儿 就只和大家说说偶数阶的了)。


3阶
81

这篇关于N(奇数)阶幻方解法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/860087

相关文章

力扣刷题 杨辉三角(使用c++ vector解法)

杨辉三角 题目描述示例1示例2提示:代码 题目描述 给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。 在「杨辉三角」中,每个数是它左上方和右上方的数的和。 示例1 输入: numRows = 5 输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] 示例2 输入: numRows = 1

【java编程(在线笔试)】【链表】两道k个一组翻转链表题目(包含非递归和递归两种解法)

一、给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点也翻转顺序。 1. 非递归解法 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) {

每天刷个算法题20160525:快速排序的递归转非递归解法

版权所有。所有权利保留。 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/51524798 为了防止思维僵化,每天刷个算法题。已经刷了几天了,现在发点代码。 我已经建了一个开源项目,每天的题目都在里面: https://github.com/Xiaofei-it/Algorithms

每天刷个算法题20160524:阿克曼函数的递归转非递归解法

版权所有。所有权利保留。 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/51524754 为了防止思维僵化,每天刷个

n^3 连续奇数和 java

任何一个自然数m的立方都可以写成连续奇数之和。 如: 1^3=1 2^3=3+5; 3^3=7+9+11 请编程实现;任一自然数n,求组成n^3。 import java.util.Scanner;public class CiFangQiShuHe {public static void main(String[] args) {Scanner input=

LeetCode第21题之Generate Parentheses(两种解法)

C++代码: 解法一(在LeetCode上运行效率高于解法二): #include <vector>#include <iostream>#include <string>using namespace std;class Solution {private:vector<string> res;public: //leftRemain保存还可以放左括号的数目,rightRemain

反转链表的解法分享

1、双指针法 ListNode* addInList(ListNode* head1, ListNode* head2) {// write code hereListNode* ReverseList(ListNode* pHead){if(pHead == NULL)return NULL;ListNode* cur = pHead;ListNode* pre =NULL;while(cu

3Sum Closest问题及解法

问题描述: Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have

Longest Common Prefix问题及解法

问题描述: Write a function to find the longest common prefix string amongst an array of strings. 问题分析: 我们只需要从头到尾把每个字符串共同的字符前缀找出即可。 详见代码: class Solution {public:string longestCommonPrefix(vec

Roman to Integer问题及解法

问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 问题分析: 我们根据罗马数的特点,从后往前循环字符串,若i代表的值小于i+1代表的值,sum值减小,否则就增加。 详见代码: class Solutio