创新互联python教程:
目前创新互联公司已为千余家的企业提供了网站建设、域名、网站空间、网站托管维护、企业网站设计、尤溪网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
编写一个 Python 程序来检查一个数字是不是一个 diarium 数字,或者是否使用 while 循环。如果一个给定数的数字加到其各自位置的幂的和等于实际数,它就是一个 diarium 数字。例如,175 = 11 + 72 + 53 = 1 + 49 + 125 = 175。
在这个 Python 程序中,我们将数字分成单个数字,并求出总和。接下来,if 语句检查总和是否等于实际数字。如果是真的,这是一个灾难性的数字。
Number = int(input("Enter the Number to Check Disarium Number = "))
length = len(str(Number))
Temp = Number
Sum = 0
rem = 0
while Temp > 0:
rem = Temp % 10
Sum = Sum + int(rem**length)
Temp = Temp // 10
length = length - 1
print("The Sum of the Digits = %d" %Sum)
if Sum == Number:
print("\n%d is a Disarium Number." %Number)
else:
print("%d is Not a Disarium Number." %Number)
在这个 Python 例子中,我们使用了 digitsCount 和 digitsSum 来检查这个数字是否是一个异数。
import math
def digitsCount(Number):
length = 0
while Number != 0:
length = length + 1
Number = Number // 10
return length
def digitsSum(Number, length):
Sum = 0
rem = 0
while Number > 0:
rem = Number % 10
Sum = Sum + math.pow(rem, length)
Number = Number // 10
length = length - 1
return Sum
Number = int(input("Enter the Number to Check Disarium Number = "))
length = digitsCount(Number)
Sum = digitsSum(Number, length)
print("The Sum of the Digits = %d" %Sum)
if Sum == Number:
print("%d is a Disarium Number." %Number)
else:
print("%d is Not a Disarium Number." %Number)
Enter the Number to Check Disarium Number = 175
The Sum of the Digits = 175
175 is a Disarium Number.
Enter the Number to Check Disarium Number = 24
The Sum of the Digits = 18
24 is Not a Disarium Number.
Python 程序检查一个数字是不是一个 diarium 数字或者没有使用递归。
import math
length = 0
Sum = 0
def digitsCount(Number):
global length
if Number > 0:
length = length + 1
digitsCount(Number // 10)
return length
def digitsSum(Number, length):
global Sum
if Number > 0:
rem = Number % 10
Sum = Sum + math.pow(rem, length)
digitsSum(Number // 10, length = length - 1)
return Sum
Number = int(input("Enter the Number to Check Disarium Number = "))
length = digitsCount(Number)
Sum = digitsSum(Number, length)
print("The Sum of the Digits = %d" %Sum)
if Sum == Number:
print("\n%d is a Disarium Number." %Number)
else:
print("%d is Not a Disarium Number." %Number)
Enter the Number to Check Disarium Number = 98
The Sum of the Digits = 73
98 is Not a Disarium Number.
Enter the Number to Check Disarium Number = 135
The Sum of the Digits = 135
135 is a Disarium Number.
当前名称:Python程序:检查数字是否是diarium数字
URL网址:http://www.shufengxianlan.com/qtweb/news17/153567.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联