1.求所有三位水仙花数(水仙花数就是:个位的立方➕十位的立方➕百位的立方=这个数本身)
num = 100 while num < 1000: gewei = num % 10 shiwei = num // 10 % 10 baiwei = num // 100 if gewei ** 3 + shiwei ** 3 + baiwei ** 3 == num: print(f"{num}是水仙花数") num += 1 for num in range(100, 1000): gewei = num % 10 shiwei = num // 10 % 10 baiwei = num // 100 if gewei**3 + shiwei**3 + baiwei**3 == num: print(f"{num}是水仙花数")
2.用循环求10!
a = 1 b = 10 for i in range(1, b+1): a = a * i print(a)
3.用循环求10!+9!+8!……1!
a = 1 b = 10 sum = 0 for i in range(1, b+1): a = i * a sum += a print(sum)