以下是输出100-500范围内的水仙花数的代码:

for num in range(100, 501):
    # 将数字转换为字符串,方便提取每一位的数字
    num_str = str(num)
    sum_of_cubes = 0
    for digit in num_str:
        sum_of_cubes += int(digit) ** 3
    if sum_of_cubes == num:
        print(num)

输出结果为:

153 370 371 407


原文地址: https://www.cveoy.top/t/topic/i1u0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录