Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. n = int(input('Размер: '))
  2. a = []
  3. for i in range(n):
  4.     a.append([0,] * n)
  5. left, right, top, bott = 0, n - 1, 0, n - 1
  6. i, j, cnt, state = 0, 0, 1, 1
  7. if n > 3:
  8.     bar = n * n // 2 + (n - 2)
  9. elif n == 3:
  10.     bar = n * n // 2 + 2
  11. else:
  12.     bar = n + 1
  13. while cnt <= bar:
  14.     if state == 1:
  15.         if i == top and j == right:
  16.             state = 2
  17.             a[i][j] = cnt
  18.             cnt += 1
  19.             top += 1
  20.             right -= 2
  21.             j = right + 1
  22.             i = top
  23.             continue
  24.         a[i][j] = cnt
  25.         cnt += 1
  26.         j += 1
  27.     elif state == 2:
  28.         if i == bott and j == left:
  29.             state = 3
  30.             a[i][j] = cnt
  31.             cnt += 1
  32.             bott -= 1
  33.             j = left
  34.             i = bott
  35.             continue
  36.         a[i][j] = cnt
  37.         cnt += 1
  38.         i += 1
  39.         j -= 1
  40.     elif state == 3:
  41.         if i == top and j == left:
  42.             state = 1
  43.             a[i][j] = cnt
  44.             cnt += 1
  45.             left += 1
  46.             j = left
  47.             i = top
  48.             continue
  49.         a[i][j] = cnt
  50.         cnt += 1
  51.         i -= 1
  52. for i in range(len(a)):
  53.     print(a[i])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement