螺旋数字的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 ...
随机推荐
- ekhtml使用总结
ekhtml是一个高效SAX方式的HTML解析库. 文件说明 官网下载ekhtml-0.3.2.tar.gz文件解压后,内部包括源码.测试文件.文档.编译脚本等. 如需编译成静态库或动态库后进行集成, ...
- [SHOI 2009] 会场预约
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2028 [算法] 直接用std :: set维护即可 时间复杂度 : O(NlogN) ...
- ASP.NET Core:目录
ylbtech-ASP.NET Core:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http:// ...
- Android webkit 事件传递流程通道分析
前言:基于android webview 上定制自己使用的可移植浏览器apk,遇到好多按键处理的问题.所以索性研究了一下keyevent 事件的传递流程. frameworks 层 keyevent ...
- Codechef LOCAUG17
做完题目很少有写题解的习惯,强行PO一组吧. 比赛链接:https://www.codechef.com/LOCAUG17 PRINCESS 给定字符串s,问s是否存在长度大于1的回文子串. 解:分两 ...
- hibernate基础配置文件
主配置文件 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC " ...
- 51nod1432【贪心】
对于每个数我找一个和他相加最接近独木舟,然后ans+=1; 想复杂了,直接两端来就好了. 然后两个相加如果<=m那么就让它们在一起,不是的话就让大的一艘船,然后继续搞(贪心) #include ...
- pojcoin【未完待续】
题意: 给你一些数字的种类,然后拥有这个种类的各个数量,输出可以组成多少数字,数字范围在1-m: 思路: 卧槽好难-
- PTA 模拟,【放着一定要写哈哈哈哈哈】(据说用string哟)
实现一种简单原始的文件相似度计算,即以两文件的公共词汇占总词汇的比例来定义相似度.为简化问题,这里不考虑中文(因为分词太难了),只考虑长度不小于3.且不超过10的英文单词,长度超过10的只考虑前10个 ...
- 用动态链表high-poj 1528
//2333333 题目超级水,但是!刚学了链表拿来high一high也不错啊. #include <iostream> #include <stdio.h> #include ...