熟悉python,也整一个类似java实现的,功能基本一样,练练手,,
也可以参考另外一篇java的实现文章,
内螺旋数组java实现
源码如下:
#-*-coding:UTF-8-*- #!/usr/bin/python ##array define ##dennis zhao 27202787@qq.com ##2013-08-02 import time,sys result = [[] for i in range(5)] initial = [[] for i in range(5)] directions = [[] for i in range(5)] print ("="*60); for k in range(0,5): for j in range(1,6): result[k].append(0); for k in range(0,5): for j in range(1,6): initial[k].append(0); for k in range(0,5): for j in range(1,6): directions[k].append('-'); def showData(ll,dirs): ret=""; for i in range(len(ll)): s=str(ll[i]); ret=ret + s.zfill(2)+ dirs[i] +"\t"; return ret; #print ("result",result) #print ("initial",initial) #print ("directions",directions) count=0 row = 0; col = -1; while (count < 5*5): #left to right col +=1; while(col<5 and initial[row][col] == 0): initial[row][col]=1; count+=1; result[row][col] = count; directions[row][col] = "→"; col+=1; #print ("first", result); #time.sleep(1); col-=1;#last column minus 1 row+=1;#top to down while (row < 5 and initial[row][col] == 0): initial[row][col] = 1; #global count; count+=1; result[row][col] = count; directions[row][col] = "↓"; row+=1; row-=1;#last row minus 1 #print ("second", count); #time.sleep(1); col-=1;#right to left while (col >= 0 and initial[row][col] == 0): initial[row][col] = 1; count+=1; result[row][col] = count; directions[row][col] = "←"; col-=1; col+=1;# last column add 1 #print ("third", count); #time.sleep(1); # down to top row-=1; while (row >= 0 and initial[row][col] == 0) : initial[row][col] = 1; count+=1; result[row][col] = count; directions[row][col] = "↑"; row-=1; row+=1;# last row add 1 #print ("fourth", count); #time.sleep(1); for index in range(len(result)): print ('Line value (',index+1,'):\t', showData(result[index],directions[index])) #print ("result",result) #print ("initial",initial) #print ("directions",directions) print ("="*60);
效果图:
相关推荐
java螺旋数组源码java螺旋数组源码java螺旋数组源码
用 C 编的螺旋数组 用 C 编的螺旋数组 用 C 编的螺旋数组 用 C 编的螺旋数组 用 C 编的螺旋数组
利用四个for循环实现逆时针和顺时针的螺旋数组
螺旋数组的java实现-----------------------------------------------------------------------------------------------------------
c++螺旋数组
#include"stdio.h" #include"iostream.h" int array[11][11]; int temp; int ROW; void godown(int &m,int &a) { for(temp=1;temp;temp++) if(array[temp][a]==0) array[temp][a]=m++;...} }
昨天去面试 公司出了一题螺旋数组 输入 5 输出 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 当场让我编写,当时比较紧张 没有写出来,回来想了想 其实也不难,附上代码实现,希望能帮到...
解决输出顺时针螺旋数组问题【寻路算法】C#代码 面试题
大家都来下下..改进改进 我没积分了...我要下东西.... 大家都支持支持...
java螺旋数组有关Java面向对象编程指南 Java面向对象编程指南
用vc++6.0编写实现螺旋数组,并经过调试之后可以使用,欢迎大家的下载。
用Visual 2008编写的控制台应用程序,实现了螺旋数组的一些简单的功能
C语言实现的螺旋数组,资源里给出了程序及标准答案,大家可以在这个基础上改进
螺旋数组问题,相信很多人都做过了,初学者可以看一下,应该会有用处的。
彩色螺旋图形python源代码
螺旋矩阵--数组的运用,昨晚后我还是满有成就感的...刚学程序。不容易啊
螺旋输出二维数组中的各个元素,不错的,可以直接运行的
用C++实现将数组中元素以螺旋状顺序打印出来,包含代码以及介绍文档
本代码是用c语言编写而成,实现了一个螺旋形状的方阵,所谓螺旋方阵就是一个二维数组,数组的值按照由外到内从小到大螺旋上升的一个数组方阵。