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 20:28:45 浏览: 49
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)
阅读全文