import math # math.pow(x,y) 求x的y次幂 num= input("输入一个数····") g_num = 0 index =[] rec = [] for i in str(num): # 取到各个数位上的数字并加入index列表 index.append(int(i)) # print(index) # 获取列表长度 length = len(index) for a in range(0,length): rec.append(int(math.pow(index[a],length))) for i in range(0,length): g_num += rec[i] print("结果为%d"%g_num) ifnot g_num == int(num): print("这是一个阿姆斯特朗数·· ") else: print("输入的数值非阿姆斯特朗数.....")
# 方法1 defnarcissistic_number_1(num): length = len(str(num)) count = length num_sum = 0 while count: num_sum += ((num // 10 ** (count - 1)) % 10) ** length # 注意运算符的优先级 count -= 1 if num_sum == num: print("%d is %d bit narcissistic_number" % (num, length)) else: print("is not a narcissistic number")
# 方法2 defnarcissistic_number_2(num): s = str(num) length = len(s) sum_num = 0 for i in s: sum_num += int(i)**length if sum_num == num: print("%d is a %d bit narcissistic_number" % (num, length) max_num = int(input('请输入最大范围')) # 获取小于指定数的阿姆斯特朗数 for num in range(0, max_num): narcissistic_number_1(num) #调用方法一,方法二均可