Python知識分享網(wǎng) - 專業(yè)的Python學習網(wǎng)站 學Python,上Python222
Python 循環(huán)綜合案例-求水仙花數(shù)
發(fā)布于:2023-09-11 15:10:33

Python 7天快速入門完整視頻教程https://www.bilibili.com/video/BV1o84y1Z7J1

 

Python   循環(huán)綜合案例-求水仙花數(shù)

 

 

水仙花數(shù)是指一個 3 位數(shù),它的每個數(shù)位上的數(shù)字的 3次冪之和等于它本身。例如:1^3 + 5^3+ 3^3 = 153。

參考代碼:

 

# 數(shù)字xyc
for x in range(1, 10):  # 百位數(shù)x 取值1-9
    for y in range(0, 10):  # 十位數(shù)y 取值0-9
        for z in range(0, 10):  # 個位數(shù)z 取值0-9
            s1 = x * 100 + y * 10 + z  # 本身值
            s2 = x ** 3 + y ** 3 + z ** 3  # 每個數(shù)位上的數(shù)字的3次冪之和
            if s1 == s2:
                print(f"水仙花有:{s1}")

 

作業(yè):用while循環(huán)實現(xiàn)求水仙花數(shù)

 

 

轉載自: