Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- n = int(input('Размер: '))
- a = []
- for i in range(n):
- a.append([0,] * n)
- left, right, top, bott = 0, n - 1, 0, n - 1
- i, j, cnt, state = 0, 0, 1, 1
- if n > 3:
- bar = n * n // 2 + (n - 2)
- elif n == 3:
- bar = n * n // 2 + 2
- else:
- bar = n + 1
- while cnt <= bar:
- if state == 1:
- if i == top and j == right:
- state = 2
- a[i][j] = cnt
- cnt += 1
- top += 1
- right -= 2
- j = right + 1
- i = top
- continue
- a[i][j] = cnt
- cnt += 1
- j += 1
- elif state == 2:
- if i == bott and j == left:
- state = 3
- a[i][j] = cnt
- cnt += 1
- bott -= 1
- j = left
- i = bott
- continue
- a[i][j] = cnt
- cnt += 1
- i += 1
- j -= 1
- elif state == 3:
- if i == top and j == left:
- state = 1
- a[i][j] = cnt
- cnt += 1
- left += 1
- j = left
- i = top
- continue
- a[i][j] = cnt
- cnt += 1
- i -= 1
- for i in range(len(a)):
- print(a[i])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement