第1题: 99乘法表
def nn_table():
for i in range(1, 10):
for j in range(1, i+1):
# print("{}*{}={}\t".format(i, j, i*j), end=" ")
print( "%d*%d=%2d\t" %(i, j, i*j), end="")
print(" ")
第2题:计算 1~100之间奇数的和
def cal_1_100():
suma = 0
for i in range(1, 101):
if i % 2 != 0:
suma += i
return suma
第3题:汉诺塔a,b,c三个柱子,a表示初始初始位,b表示过渡位,c表示目标位。
n表示初始位置a上有多少个圆盘。
def move_hn(n, a, b, c):
if n == 1:
print(a, "-->", c)
else:
move_hn(n-1, a, c, b) # 将前 n-1 个圆盘从 a移动到 b。此时a为初始位,c为过渡位,b为目标位。
move_hn(1, a, b, c) # 将底下最后一个圆盘从 a移动到 c。此时a为初始位,b为过渡位,c为目标位。
move_hn(n-1, b, a, c) # 将 b 上的 n-1 个圆盘移动到 c。此时b为初始位,a为过渡位,c为目标位。