螺旋数字的python实现
螺旋数字的算法简单实现。
示例 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*steps def 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] = curSeed def 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 traceback
try:
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")
螺旋数字的python实现的更多相关文章
- 猜数字游戏-python
		题目: 用python写一个猜数字的游戏,游戏规则如下: 1.由一个人随机写一个整数1-99(如:21) 2.一群小伙伴轮流猜数字,如第一个人猜一个数(如:48),则缩小范围至(1-48) 3.如第二 ... 
- 一起来刷《剑指Offer》-- 题目一:找出数组中重复的数字(Python多种方法实现)
		数组中重复的数字 最近在复习算法和数据结构(基于Python实现),然后看了Python的各种"序列"--比如列表List.元组Tuple和字符串String,后期会写一篇博客介绍 ... 
- KNN识别图像上的数字及python实现
		领导让我每天手工录入BI系统中的数据并判断数据是否存在异常,若有异常点,则检测是系统问题还是业务问题.为了解放双手,我决定写个程序完成每天录入管理驾驶舱数据的任务.首先用按键精灵录了一套脚本把系统中的 ... 
- 一串数字中,只有一个数字出现一次,其他数字都出现两次,查找出这个数字(python)(原创)
		背景: 电话面试&手撕代码 2019.03.22 Mufasa 问题: 一串数字中,只有一个数字出现一次,其他数字都出现两次,查找出这个数字 条件: 这串数字是有序数 解决方法: 核心代码只有 ... 
- Leetcode 268.缺失数字 By Python
		给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [9,6,4,2 ... 
- 猜数字问题 python
		猜数字问题,要求如下: ① 随机生成一个整数 ② 猜一个数字并输入 ③ 判断是大是小,直到猜正确 ④ 判断时间提示:需要用time模块.random模块该题目不需要创建函数 import random ... 
- 剑指offer-数组中重复的数字-数组-python
		题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ... 
- 剑指offer-数组中只出现一次的数字-数组-python
		题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. # -*- coding:utf-8 -*- class Solution: # 返回[a, ... 
- leetcode 374. 猜数字大小(python)
		我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你需要猜我选择了哪个数字.每次你猜错了,我会告诉你这个数字是大了还是小了.你调用一个预先定义好的接口 guess(int n ... 
随机推荐
- ubuntu中使用apt-get安装zbar
			apt-get是linux中常用的shell命令,适用于deb包管理式的操作系统,主要用于自动从互联网的软件仓库中搜索.安装.升级.卸载软件或操作系统.apt-get命令一般需要root权限执行,所以 ... 
- SPOJ:Divisors of factorial (hard) (唯一分解&分块优化)
			Factorial numbers are getting big very soon, you'll have to compute the number of divisors of such h ... 
- [POI 2007] 办公楼
			[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1098 [算法] 显然 , 答案为补图的连通分量个数 用链表优化BFS , 时间复杂度 ... 
- C. Vanya and Scales
			time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ... 
- 009--test命令和grep作业及Sed作业awk作业和循环结构
			一.test命令 -d :目录 test -d /boot -s :文件长度 > 0.非空 test - ... 
- Win10应用设计的那些事儿
			此文已由作者杨凯明授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 继Windows 10系统发布之后,很多Windows用户更新了系统.win10系统的发布,在以往的metr ... 
- C++笔试题(三)
			普天是南京一家通信公司,全称为:南京普天通信股份有限公司,公司网址为:http://www.postel.com.cn 网上流传一套普天C++笔试题,我将我做的答案公布与此,仅供参考. 1.实现双向链 ... 
- bzoj 3926: [Zjoi2015]诸神眷顾的幻想乡【SAM】
			有一个显然的性质就是每个串一定在某个叶子为根的树中是一条直的链 然后因为SAM里是不会有相同状态的,所以以每个叶子为根dfs一遍,并且动态构造SAM(这里的节点u的last指向父亲),最后统计答案就是 ... 
- GraphicsLab Project学习项目
			作者:i_dovelemon 日期:2016 / 05 / 30 主题:3D,Graphics 引言 进公司以来,主要在学习的就是如何保证代码的质量,以前热爱的图形学也放置了.但是,作为游戏程序员,特 ... 
- jquery  html() 和text()的用法
			html()类似JS中的 innerHTML,首先看一段代码: <!DOCTYPE html> <html lang="en"> <head> ... 
