朋友給了我一個很 Python 的解法,要用 Python 3.8 以上才能執行
from itertools import product, zip_longest
from math import prod
def show99(f):
if f[0][1]==9: return f"{f[0][1]:1d} x {f[0][0]:1d} = {f[1]:2d}\n"
else: return f"{f[0][1]:1d} x {f[0][0]:1d} = {f[1]:2d}, "
e = list(product(range(1,10), repeat=2))
l = list(map(prod, e))
f = zip_longest(e,l)
print(''.join(map(show99,f)))