python水仙花数100~999
作者:野牛程序员:2023-07-08 14:01:13python阅读 2786
水仙花数是指一个三位数,它的每个位上的数字的立方和等于该数本身。以下是100到999之间的所有水仙花数的Python代码:
for num in range(100, 1000): # 分离百位、十位和个位数字 hundreds = num // 100 tens = (num // 10) % 10 ones = num % 10 # 判断是否为水仙花数 if num == (hundreds ** 3) + (tens ** 3) + (ones ** 3): print(num)
运行上述代码,将输出所有的水仙花数:
153 370 371 407
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

- 上一篇:求100~999之间的水仙花数
- 下一篇:回文数的数学性质
