螺旋数字的算法简单实现。

示例 5

01 02 03 04 05

16 17 18 19 06

15 24 25 20 07

14 23 22 21 08

13 12 11 10 09

通过观察,外部数字进行环绕一圈后向内收拢。

从程序出发,只要递归处理好4条边即可。

同时为了避免顶点重复赋值,最后一个点让后续的边处理。

说明:处理暂时存储在一个list对象中。

实现代码:

def getlocIndex(l_x,l_y,steps):       return l_x  + l_y*stepsdef increaseSeedAndSteps(curSeed,cur_steps):return (curSeed +1,cur_steps+1)def setTargetItem(targetlst,l_cur_x,l_cur_y,steps,curSeed):loc_index = getlocIndex(l_cur_x, l_cur_y, steps)targetlst[loc_index] = curSeeddef calc(targetlst,seed,l_x,l_y,nextsteps,steps):       current_seed = seed    loop_steps = nextsteps-1if( nextsteps < 1 ):              setTargetItem(targetlst, l_x, l_y,steps, current_seed)              returneach_steps = 0while(each_steps <= loop_steps):                                                              setTargetItem(targetlst, l_x+each_steps, l_y,steps, current_seed)        current_seed,each_steps = increaseSeedAndSteps(current_seed ,each_steps)each_steps = 0while(each_steps <= loop_steps):            setTargetItem(targetlst, l_x+nextsteps, (l_y+each_steps), steps, current_seed)                   current_seed,each_steps = increaseSeedAndSteps(current_seed ,each_steps)          each_steps = 0while(each_steps <= loop_steps):                                                        setTargetItem(targetlst, l_x+nextsteps-each_steps, l_y+nextsteps, steps, current_seed)          current_seed,each_steps = increaseSeedAndSteps(current_seed ,each_steps)            each_steps = 0while(each_steps <= loop_steps):                                                        setTargetItem(targetlst, l_x, l_y+nextsteps-each_steps, steps, current_seed)                         current_seed,each_steps = increaseSeedAndSteps(current_seed ,each_steps)if(nextsteps-2 >= 0):calc(targetlst,current_seed,l_x+1,l_y+1,nextsteps-2,steps)

  

测试代码:

def outputResult(targetlst,steps):outBuffer = ''for rowIndex in range(0, steps* steps):if(rowIndex % steps == 0 and len(outBuffer) >0):print('%s\n' % (outBuffer))              outBuffer = ''outBuffer = outBuffer + '%02d ' %(targetlst[rowIndex])print('%s\n' % (outBuffer))               import traceback
try:steps =5targetlst = list()[ targetlst.append(0) for nTry in range(0,steps* steps)]       calc(targetlst, 1,0,0,steps-1,steps)outputResult(targetlst, steps)except Exception as exc:           print("app catch: %s\n" % ( exc));   info = traceback.format_exc()print(info)
print("done")

  

转载于:https://www.cnblogs.com/febwave/p/4645036.html

螺旋数字的python实现相关推荐

  1. python数字类型-Python数字类型及其操作

    数字类型 Python 语言提供了3种数字类型:整数.浮点数和复数. 布尔型 In addition, Booleans are a subtype of integers. 整数类型(int)与数学 ...

  2. python数字类型-Python基础:数据类型-数字(5)

    在Python中,所有的数据类型都是类,每一个变量都是类的实例. Python中有6种标准数据类型:数字(Number).字符串(String).列表(List).元组(Tuple).集合(Sets) ...

  3. 检查字符串是否包含数字的Python程序

    Given a string and we have to check whether it contains only digits or not in Python. 给定一个字符串,我们必须检查 ...

  4. python语言的三种数字类型_Python语言中的类型之数字类型--Python(10)

    数据从不一样的角度能够用不一样的含义去解释它,如: 10,011,101 对上面这个数据,咱们有以下多种含义去解释它: 1)它是1个二进制数字,或者是1个十进制数字: 2)一段文本: 3)用 , 号分 ...

  5. 练习:数字时钟(Python 自定义类)

    Python 官网:https://www.python.org/ Free:大咖免费"圣经"教程< python 完全自学教程>,不仅仅是基础那么简单-- My CS ...

  6. 用二分法编写猜数字游戏(含:猜电脑随机数,和用户自己想的数字)python

    目录 #什么是二分法 #用二分法猜电脑随机数 #用二分法猜用户所想的数 1.什么是二分法? 对于区间[a,b]上连续不断且f(a)·f(b)<0的函数y=f(x),通过不断地把函数f(x)的零点 ...

  7. leetcode-136-只出现一次的数字(java|python)

    title: leetcode-136-只出现一次的数字(java|python) date: 2019-09-25 19:12:13 mathjax: true categories: leetco ...

  8. 蓝桥杯 ALGO-1005 数字游戏 python

    蓝桥杯 ALGO-1005 数字游戏 python 试题 算法训练 数字游戏 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个1-N的排列a[i],每次将相邻两个数相加,得到 ...

  9. 简单数字识别 python+tesseract-ocr

    简单数字识别 python+tesseract-ocr 需求 实现一个简单数字图片识别功能,并在PC端跑起来 场景 相机拍照数据图片,程序识别出图片上简单数据并返回 工具 pycharm 环境 pyt ...

  10. python数字类型-Python数字类型有哪些

    python中数字类型有分为如下几种类型:整型 - int 长整型 - long 浮点型 - float 复数型 - complex Python是一种弱类型语言,所以变量都是不需要提前声明,可以直接 ...

最新文章

  1. 看动画轻松理解「链表」实现「LRU缓存淘汰算法」
  2. SQL Server连接中的常见错误
  3. Chrome Version 19.0.1055.1 dev Flash Missing plug-in的修复
  4. oracle备份信息在控制文件丢失,恢复之利用备份在所有控制文件丢失情况下恢复(一)...
  5. 运维在浏览器搜索linux日志,Graylog日志管理系统---搜索查询方法使用简介
  6. DELL戴尔服务器RAID磁盘阵列默认识别更换后的硬盘
  7. python 连接sql server
  8. 【转】【C#.NET】ASP.NET状态管理 APPlication,Session,Cookie和ViewStat用法
  9. c语言程序设计高校饭卡系统,C语言课程设计-学生饭卡管理系统(45页)-原创力文档...
  10. endnotex8与9的区别_下载安装EndnoteX8或EndnoteX9,建立数据库并以自己的名字命名。...
  11. win10计算机分盘怎么设置密码,Win10如何限制磁盘分区被访问 Win10自带磁盘加密功能BitLocker在哪里...
  12. Conditional Generative Adversarial Nets(CGAN)
  13. HTML超链接标签—链接QQ在线聊天
  14. fast无线路由器设置服务器,迅捷(FAST)路由器静态ip上网设置方法
  15. hadoopsnappy解压_配置Hadoop2.7.2和Hbase1.1.5支持Snappy解压压缩库
  16. 如何在指板上寻找特殊和弦
  17. 餐厅菜单html代码,html5css3 3D餐厅菜单概念_订餐菜单选择工具
  18. 数据库------DQL操作
  19. 高等教育心理学:问题解决与创造性
  20. 关于LLC的文献总结

热门文章

  1. 8、饼图(ax.pie)
  2. cognos数据源配置修改oracle,Cognos 新建数据源报错(原创)
  3. java debug try catch 打印发生错误的代码的详细信息 代码行数
  4. FISCO BCOS 区块最大最长交易执行时间
  5. FISCO BCOS源码(1)代码目录结构
  6. vue—点击换一批就更换一批内容_Vue.js点击切换按钮改变内容的实例讲解
  7. android sd卡 格式化 rom,Android 系统获取内存,ROM,SD卡大小
  8. 基于SSM的在线商城系统(最新)
  9. linux中可以使用-af含义,关于Windows中的linux:AF_UNIX
  10. Servlet期末复习笔记3