"""
2 问题描述:
3 给定一个奇数(num),生成一个横竖斜加起来的和相等
4 问题解析:
5 这其实就是一个九宫格的问题
6 九宫格问题的解答技巧:
7 1要放在第一行的正中央,每次按依次向上的格式去填值,如果超出上边框就 把它竖直往下移到最后一行,如果超出右边框就把它横着移动到最左边,在> 移动过程中如果要移动的地方已经存在值,就把这个放到前一个数字的下面> ,如果上边框和右边框都超过啦,就和排重一样,也把这个放到前一个数字> 的下面。
8 问题解决步骤:
9 根据我们发现的规律来进行代码化
10 1.创建一个缺省值为0的num*num的二维列表
11 2.根据根据规律来书写代码
12 """
13 #创建num*num的二维列表
14 num = int(input())
15 a = [[0 for i in range(num)]for i in range(num)]
16 #将第一行中间那列的值定为1
17 a[0][num//2] = 1
18 x = 0
19 y = num//2
20 #将2以后的值依次填入列表中
21 for i in range(1,num*num):
22 j = i + 1
23 x = x - 1
24 y = y + 1
25 if x < 0 and y <= num - 1:
26 x = x + 3
27 a[x][y] = j
28 elif x < 0 and y > num - 1:
29 x = x + 2
30 y = y - 1
31 a[x][y] = j
32 elif y > num - 1:
33 y = 0
34 a[x][y] = j
else:
36 if a[x][y] == 0:
37 a[x][y] = j
38 else:
39 x = x + 2
40 y = y - 1
41 a[x][y] = j
42
43 for n in range(num):
44 for m in range(num):
45 print(str(a[m][n]).rjust(2,"0"),end = "\t")
46 print()

运行结果

08      03      04
01      05      09

06      07      02

当输入5时

15      09      24      00      00
10      25      16      17      18

01      06      11      19      20

07      12      02      21      22

13      14      08      23      00

如果出现看代码无法理解逻辑时,则就是没有充分的了解这种数独的解题技巧,可以百度其解题技巧后再返回来看代码逻辑则可!

python打印9宫格25宫格81宫格.....的更多相关文章

  1. Python打印格式化与字符串

    关于Python打印格式化与字符串,比较全面的总结,希望对大家有帮助~ # -*- coding: cp936 -*- ''' 打印格式 ''' print "a" print & ...

  2. 【Python】Python 打印和输出更多用法。

    Python 打印和输出 简述 在编程实践中,print 的使用频率非常高,特别是程序运行到某个时刻,要检测产生的结果时,必须用 print 来打印输出. 关于 print 函数,前面很多地方已经提及 ...

  3. python 打印 九九表

    用Python 打印九九表. print 每打印一行默认会带有换行, 在print语句后加上,end = 't' 会变成tab. 排版会好点. def main(): for i in range(1 ...

  4. BootStrap入门教程 (一) :手脚架Scaffolding(全局样式(Global Style),格网系统(Grid System),流式格网(Fluid grid System),自定义(Customing),布局(Layouts))

    2011年,twitter的“一小撮”工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用.优雅.灵活.可扩展的前端工具集--BootStrap.Bootstrap由MARK ...

  5. python打印表格式数据,留出正确的空格和段落星号或注释

    python打印表格式数据,留出正确的空格,格式化打出 代码如下: def printPicnic(itemsDict,leftWidth,rightWidth): print('PICNIC ITE ...

  6. python 打印 emoji

    python 打印 emoji 如需转发,请注明出处:小婷儿的python  https://www.cnblogs.com/xxtalhr/p/10486506.html 一.Unicode字符集: ...

  7. python打印列表的下标和值的例子:

    python打印列表的下标和值的例子: In [1]: list01=[1,4,5] In [10]: def funct01(ll):   ....:     for index,value in ...

  8. python打印表格式数据-星号或注释

    python打印表格式数据,留出正确的空格,格式化打出 代码如下: def printPicnic(itemsDict,leftWidth,rightWidth): print('PICNIC ITE ...

  9. python 打印 str 字符串的实际内容 repr(str)

    python 打印 str 字符串的实际内容 repr(str) s = 'aa' print(repr(s))

  10. python打印日志log

    整理一个python打印日志的配置文件,是我喜欢的格式. # coding:utf-8 # 2019/11/7 09:19 # huihui # ref: import logging LOG_FOR ...

随机推荐

  1. C# LINQ (2)

    Limiting Data -- Take() and Skip() 前面讲了 筛选 和 排序,现在讲 选取皇帝选妃,层层选拔,最后留几个,让他过目,他选一个或者几个作为妃子,大概是这么个意思Take ...

  2. 快速计算类似斐波那契数列数列的数列的第N项,矩阵快速幂

    这个题有循环节,可以不用这么做,这个可以当一个模板 #include <iostream> #include <cstdio> using namespace std; typ ...

  3. unity编辑器扩展学习

    扩展编辑器实际上就是在unity菜单栏中添加一些按钮,可以一键执行一些重复性的工作. 一.添加按钮 1.简单使用MenuItem特性 using UnityEngine; using UnityEdi ...

  4. Android Activity All In One

    Android Activity All In One Android Activity Lifecycle https://developer.android.com/reference/andro ...

  5. cookie & maxAge & expires

    cookie & maxAge & expires Expires (timestamp) & Max-Age (seconds) https://developer.mozi ...

  6. bind & this & new & arrow function

    bind & this & new & arrow function this bind call apply new arrow function arrow functio ...

  7. how to write a node cli tool

    how to write a node cli tool https://www.google.com/search?q=how+to+node+cli+tool&oq=how+to+node ...

  8. Chrome V8 系统架构

    Chrome V8 系统架构 Chromium 多进程多线程架构 design-documents https://www.chromium.org/developers/design-documen ...

  9. flutter 插件调用callback函数

    dart plugin class TestLib { static MethodChannel _channel = const MethodChannel('test_lib') ..setMet ...

  10. Objec.assign & bug

    Objec.assign & bug shallow copy https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Referenc ...