結果
問題 |
No.401 数字の渦巻き
|
ユーザー |
![]() |
提出日時 | 2018-03-08 23:21:53 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 29 ms / 2,000 ms |
コード長 | 1,842 bytes |
コンパイル時間 | 154 ms |
コンパイル使用メモリ | 12,544 KB |
実行使用メモリ | 10,880 KB |
最終ジャッジ日時 | 2024-10-05 08:18:18 |
合計ジャッジ時間 | 2,066 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 30 |
ソースコード
# -*- coding: utf-8 -*- """ No.401 数字の渦巻き https://yukicoder.me/problems/no/401 """ import sys from sys import stdin from itertools import cycle input = stdin.readline def next_repeat(n): """ 渦巻きを書く際のある方向への移動回数。 最初は右方向にnマス連続で移動する。次は下方向にn-1マス移動する。その次は左方向にn-1マス移動する。 これ以降は2回方向が変わるごとに移動量が-1されていく。 """ yield n while True: n -= 1 if n < 0: raise ValueError yield n yield n def next_pos(n): """ 次に数字を埋める渦巻きのx, y座標を返す """ x, y = -1, 0 # 次に数字を塗る座標。最初に移動した後に0, 0になるようにx=-1からスタート d = cycle([(1, 0), (0, 1), (-1, 0), (0, -1)]) # 渦巻きを書く際の方向、右・下・左・上の順で x, yの変化量 c = next_repeat(n) # 渦巻きの一辺の長さ while True: dx, dy = d.__next__() count = c.__next__() for _ in range(count): nx = x + dx ny = y + dy yield nx, ny x = nx y = ny def guruguru(N): """ 数字の渦巻きをつくる :param N: 一辺のサイズ :return: 完成した渦巻 """ ans = [[''] * N for _ in range(N)] p = next_pos(N) for i in range(1, N*N+1): x, y = p.__next__() ans[y][x] = '{:03d}'.format(i) # そのままプリントできるように0パディングした文字列で塗る return ans def main(args): N = int(input()) ans = guruguru(N) for row in ans: print(*row, sep=' ') if __name__ == '__main__': main(sys.argv[1:])