博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
螺旋数字的python实现
阅读量:4325 次
发布时间:2019-06-06

本文共 2902 字,大约阅读时间需要 9 分钟。

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

示例 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-1       if( nextsteps < 1 ):                            setTargetItem(targetlst, l_x, l_y,steps, current_seed)                            return       each_steps = 0       while(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 = 0       while(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 = 0       while(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 = 0       while(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 tracebacktry:       steps =5              targetlst = 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

你可能感兴趣的文章
【MyBean调试笔记】接口的使用和清理
查看>>
07 js自定义函数
查看>>
jQueru中数据交换格式XML和JSON对比
查看>>
form表单序列化后的数据转json对象
查看>>
[PYTHON]一个简单的单元測试框架
查看>>
iOS开发网络篇—XML数据的解析
查看>>
[BZOJ4303]数列
查看>>
一般处理程序在VS2012中打开问题
查看>>
C语言中的++和--
查看>>
thinkphp3.2.3入口文件详解
查看>>
POJ 1141 Brackets Sequence
查看>>
Ubuntu 18.04 root 使用ssh密钥远程登陆
查看>>
Servlet和JSP的异同。
查看>>
虚拟机centOs Linux与Windows之间的文件传输
查看>>
ethereum(以太坊)(二)--合约中属性和行为的访问权限
查看>>
IOS内存管理
查看>>
middle
查看>>
[Bzoj1009][HNOI2008]GT考试(动态规划)
查看>>
Blob(二进制)、byte[]、long、date之间的类型转换
查看>>
OO第一次总结博客
查看>>