美文网首页
python 阿姆斯特朗数(水仙花数)

python 阿姆斯特朗数(水仙花数)

作者: 吃鱼喵了个鱼 | 来源:发表于2019-06-02 13:39 被阅读0次
阿姆斯特朗数:如果一个n位正整数等于其各位数字的n次方之和
#将n位正整数是否等于其各位数字的n次方之和定义在一个函数中
def amuste(low,high):  #形参为要判定的数字范围
    for num in range(low, high + 1):
        sum = 0
        n = len(str(num))   #求正整数的位数
        temp = num
        while temp > 0:
            digit = temp % 10
            sum += digit ** n
            temp //= 10
        if num == sum:
            print(num,end = ' ')

#检查输入是否合理,并调用amuste()函数
while(True):
    try:
        lower = int(input("please input the minimum value: "))
        upper = int(input("please input the maximal value: "))
    except ValueError:
        print("please input a right number!")
        continue
    if lower>upper:
        print("input size incorrect!")
        continue
    amuste(lower,upper)
    break

输出结果为:

please input the minimum value: 1
please input the maximal value: 1000
1 2 3 4 5 6 7 8 9 153 370 371 407

相关文章

  • python 阿姆斯特朗数(水仙花数)

    阿姆斯特朗数:如果一个n位正整数等于其各位数字的n次方之和 输出结果为:

  • 菜鸟编程学习(python&C--008)

    Python 练习实例13(Python 100例) 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位...

  • 求水仙花数,python

    用python语言实现水仙花数 什么是水仙花数? 所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例...

  • 【Python】Python方法求水仙花数原码实例

    用Python编程方法求水仙花数,一行python代码就能解决问题。 先要知道什么是水仙花数,之后再去写代码,这样...

  • Python 求“水仙花数”- 来自www.codesc.net

    Python打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是...

  • Python如何输出水仙花数?如何撰写?

    所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。用Python输出水仙花数,对于有面试需求或者对算法...

  • 单老师要的问题

    水仙花数python版 C VB 素数Python版本 VB 输入十个数,从小到大 C VB 菱形 C VB 斐波...

  • 算法题目-水仙花数

    题目: 打印出所有的水仙花数 水仙花数 水仙花数(Narcissistic number)也被称为超完全数字不变...

  • 水仙花数——Python

    水仙花数打印出 100-999 所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数 字立方和等于该数本身...

  • python 水仙花数

    最近开始学习python,一个经典的习题就是构建一个判断水仙花数的函数。 然后调用上面的函数,实现一个输出从100...

网友评论

      本文标题:python 阿姆斯特朗数(水仙花数)

      本文链接:https://www.haomeiwen.com/subject/vrrxxctx.html