Python中的阿姆斯特朗数
假设我们有一个k位数字N。当每个数字的k次幂加到N时,N是一个Armstrong数字。因此,如果它是Armstrong数字,则必须返回true,否则返回false。
为了解决这个问题,我们将遵循以下步骤-
幂:=数字位数
温度:=n,res=0
当温度不为0时
res:=res+(tempmod10)^幂
temp:=temp/10//整数除法
如果res=n,则返回true,否则返回false。
示例
让我们看下面的实现以更好地理解-
import math
class Solution(object):
def poww(self,base,power):
res = 1
while power:
if power & 1:
res *= base
base *= base
power>>=1
return res
def isArmstrong(self, n):
power =int(math.log10(n)) + 1
temp = n
res = 0
while temp:
res += (self.poww(temp%10,power))
temp//=10
return res == n
ob1 = Solution()
print(ob1.isArmstrong(153))输入值
153
输出结果
true
热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬