import collections def solve_method(n: int) -> None: asc = True list_ = collections.deque() x = 1 for i in range(1, n + 1): arr = [0] * i if asc: for j in range(i): arr[j] = x x += 1 else: for j in range(i - 1, -1, -1): arr[j] = x x += 1 list_.appendleft(arr) asc = not asc res = [] head = "" for ints in list_: content = head for j in range(len(ints)): num = ints[j] content += str(num) content += "*" * (4 - len(str(num))) if j != len(ints) - 1: content += " " res.append(content) head += " " for s in reversed(res): print(s) if __name__ == "__main__": n = int(input()) solve_method(n) 改写以上代码,python
时间: 2024-02-14 16:28:45 浏览: 187
Python库 | ml_collections-0.1.0-py3-none-any.whl
5星 · 资源好评率100%
import collections
def solve_method(n):
asc = True
list_ = collections.deque()
x = 1
for i in range(1, n + 1):
arr = [0] * i
if asc:
for j in range(i):
arr[j] = x
x += 1
else:
for j in range(i - 1, -1, -1):
arr[j] = x
x += 1
list_.appendleft(arr)
asc = not asc
res = []
head = ""
for ints in list_:
content = head
for j in range(len(ints)):
num = ints[j]
content += str(num)
content += "*" * (4 - len(str(num)))
if j != len(ints) - 1:
content += " "
res.append(content)
head += " "
for s in reversed(res):
print(s)
if __name__ == "__main__":
n = int(input())
solve_method(n)
阅读全文