如果输入6,可以生成如下矩阵:
1 20 19 18 17 16
2 21 32 31 30 15
3 22 33 36 29 14
4 23 34 35 28 13
5 24 25 26 27 12
6 7 8 9 10 11
代码如下:
1 20 19 18 17 16
2 21 32 31 30 15
3 22 33 36 29 14
4 23 34 35 28 13
5 24 25 26 27 12
6 7 8 9 10 11
代码如下:
import string
import sys
def outputcirclenumber(m):
x=0
y=0
edgelayer = 0
change_direction_count = 0
clockwise =1
direction =1 #1,2,3,4
outputNum =1
odd = m%2
a = []
for j in range(0,m):
b = [0 for i in range(0,m)]
a.append(b)
#check if the current point is in the corner now,if it is ,change the direction
def checkatcorner(x,y,m,edgelayer,direction):
if x+y<1:
return 0
if x==edgelayer and y==edgelayer+1 and direction==4:
return 1
if x==m-1-edgelayer and y==edgelayer:
return 2
if x==m-1-edgelayer and y==m-1-edgelayer:
return 3
if x==edgelayer and y==m-1-edgelayer :
return 4
return 0
while True:
if checkatcorner(x,y,m,edgelayer,direction):
direction = checkatcorner(x,y,m,edgelayer,direction)
change_direction_count = change_direction_count +1
if not change_direction_count%4 and change_direction_count >0:
edgelayer = edgelayer+1
if x==0 and y==0:
a[x][y] = outputNum
outputNum = outputNum +1
if direction ==1:
x = x+1
a[x][y] = outputNum
if direction ==2:
y = y+1
a[x][y] = outputNum
if direction ==3:
x = x-1;
a[x][y] = outputNum
if direction ==4:
y = y-1;
a[x][y] = outputNum
outputNum = outputNum +1
if odd and x == y and x==m/2 and y==m/2:
break
if not odd and x==m/2-1 and y==m/2:
break
return a
#below is the main functions
if __name__ != "__main__":
print "outside call error"
sys.exit()
number = raw_input("please input the number you want to generate circle matrix:")
try:
num = int(number)
except:
print "\n\nNumber type error\n\n"
if num<=1:
print "number must greater than 1!!!"
sys.exit()
result = outputcirclenumber(num)
for i in result:
for j in i:
print string.rjust(str(j),len(str(num*num)),' '),
print
import sys
def outputcirclenumber(m):
x=0
y=0
edgelayer = 0
change_direction_count = 0
clockwise =1
direction =1 #1,2,3,4
outputNum =1
odd = m%2
a = []
for j in range(0,m):
b = [0 for i in range(0,m)]
a.append(b)
#check if the current point is in the corner now,if it is ,change the direction
def checkatcorner(x,y,m,edgelayer,direction):
if x+y<1:
return 0
if x==edgelayer and y==edgelayer+1 and direction==4:
return 1
if x==m-1-edgelayer and y==edgelayer:
return 2
if x==m-1-edgelayer and y==m-1-edgelayer:
return 3
if x==edgelayer and y==m-1-edgelayer :
return 4
return 0
while True:
if checkatcorner(x,y,m,edgelayer,direction):
direction = checkatcorner(x,y,m,edgelayer,direction)
change_direction_count = change_direction_count +1
if not change_direction_count%4 and change_direction_count >0:
edgelayer = edgelayer+1
if x==0 and y==0:
a[x][y] = outputNum
outputNum = outputNum +1
if direction ==1:
x = x+1
a[x][y] = outputNum
if direction ==2:
y = y+1
a[x][y] = outputNum
if direction ==3:
x = x-1;
a[x][y] = outputNum
if direction ==4:
y = y-1;
a[x][y] = outputNum
outputNum = outputNum +1
if odd and x == y and x==m/2 and y==m/2:
break
if not odd and x==m/2-1 and y==m/2:
break
return a
#below is the main functions
if __name__ != "__main__":
print "outside call error"
sys.exit()
number = raw_input("please input the number you want to generate circle matrix:")
try:
num = int(number)
except:
print "\n\nNumber type error\n\n"
if num<=1:
print "number must greater than 1!!!"
sys.exit()
result = outputcirclenumber(num)
for i in result:
for j in i:
print string.rjust(str(j),len(str(num*num)),' '),
转载于:https://blog.51cto.com/flandycheng/389430