Python新手学习练习册—04循环与判断(100-1000的水仙花数) ##
1.1 基础概念
水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant,
PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 n 位数(n≥3
),它的每个位上的数字的 n 次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。
1.2 练习题目
求100到1000的水仙花数
1.3代码答案
print('********** Narcissistic number from 100 to 1000 **********')
def narcissisticNum(): #定义水仙花数的函数
for x in range(100,1000):
bai=x//100 #百位
shi=(x//10)%10 #十位
ge=x%10 #个位
tmpNum=bai**3+shi**3+ge**3 #求百,十,个位立方的和
if tmpNum==x: #判断
print(tmpNum)
narcissisticNum() #调用水仙花树函数
1.4后记
- // <—-除法后的结果为去掉小数点后小数的整数
- % <—-除法后的结果的余数