给定一个正整数n,请编写一个函数,将组成n的每个数字操作相乘,直到最终乘数结果是个位数为止。返回相乘操作的总次数。
示例:
输入:39,输出:3。因为39=27,27=14,1*4=4。题目难度:简单
题目来源:codewars
def persistence(n: int) -> int:
pass
assert persistence(39) == 3
assert persistence(999) == 4
assert persistence(8) == 1
给定一个正整数n,请编写一个函数,将组成n的每个数字操作相乘,直到最终乘数结果是个位数为止。返回相乘操作的总次数。
示例:
输入:39,输出:3。因为39=27,27=14,1*4=4。题目难度:简单
题目来源:codewars
def persistence(n: int) -> int:
pass
assert persistence(39) == 3
assert persistence(999) == 4
assert persistence(8) == 1
def persistence(n):
count=0
result=n
if result<10:
return 1
while result>=10:
words=list(str(result))
result=1
for word in words:
result*=int(word)
count+=1
return count
assert persistence(39) == 3
assert persistence(999) == 4
assert persistence(8) == 1
def persistence(n: int) -> int:
times = 1
while True:
multiplier_sum = 1
for i in [int(n) for n in str(n)]:
multiplier_sum *= i
if len(str(multiplier_sum)) == 1:
return times
else:
n = multiplier_sum
times += 1
assert persistence(39) == 3
assert persistence(999) == 4
assert persistence(8) == 1
def persistence(self, n: int) -> int:
'''
给定一个正整数n,请编写一个函数,将组成n的每个数字操作相乘,直到最终乘数结果是个位数为止。返回相乘操作的总次数。
示例:
输入:39,输出:3。因为39=27,27=14,1*4=4。
:param n:
:return:
'''
times = 0
# 先得数每位相乘的结果
cur_data = n
list_data = []
while cur_data > 9:
data = cur_data
list_data.append(data)
result = 1
while data != 0:
result = result * (int(data%10))
data =int(data / 10)
times += 1
cur_data = result
print(list_data)
return times
pass
def test_persistence(self):
assert self.persistence(0) == 0
assert self.persistence(39) == 3
def persistence(n:int):
m=str(n)
if len(m)==1:
return 1
count=0
while len(m)>1:
res=1
for i in m:
res=res*int(i)
count=count+1
m=str(res)
return count
# 思路:转化为字符串相乘计数
def persistence(n):
result = 1
if len(list(str(n))) == 1:
return 1
elif len(list(str(n))) > 1:
num = 0
while True:
for i in list(str(n)):
# print(result)
result *= int(i)
# 将两位数相乘的结果赋值给n
n = result
# result重置为1,不然result值越来越大
result = 1
num += 1
# print(f"第{num}次",n)
if n < 10:
break
return num
print(persistence(39))
print(persistence(999))
print(persistence(9))
assert persistence(39) == 3
assert persistence(999) == 4
assert persistence(8) == 1
def persistence(n):
if n<10:
return 1
count = 0
while n//10!=0:
each_number = []
while n>0:
each_number.append(n%10)
n = n//10
result = 1
for each in each_number:
result *= each
n = result
count += 1
return count
当数字小于10时,直接返回1
当数字大于10时,从个位数开始将每个数字存入列表each_number中(求余+对10整除),然后遍历each_number相乘得出新的数字,计数器加一
Python参考题解:
from functools import reduce
from operator import mul
def persistence(n: int) -> int:
if len(str(n)) == 1:
return 1
count = 0
while len(str(n))>1:
n = reduce(mul, [int(c) for c in str(n)])
count += 1
return count
persistence(39)
assert persistence(39) == 3
assert persistence(999) == 4
assert persistence(8) == 1
思路:判断输入的数字是多位的,则进行while循环计算因子的乘积,直到成为单位数。顺便统计操作的次数。
Python参考题解:
def persistence(n: int) -> int:
def rec(n: int):
if n > 10:
# 计算字符串的因子乘积
total = 1
for c in str(n):
total *= int(c)
# 将乘积结果继续调用自己
return 1 + rec(total)
else:
return 0
return 1 if n < 10 else rec(n)
assert persistence(39) == 3
assert persistence(999) == 4
assert persistence(8) == 1
思路: 利用递归,实现乘积运算和统计。
使用%来获取十位与个位,然后相乘
def number(n):
# 计数器
count = 0
# while循环:在满足条件的情况下执行
while (n > 9):
# 判断是否是两位数
if (n > 9):
# 每循环一次计数器加一
count += 1
# 分解出十位的数字,因为分解后等于小数,转换成int整数类型
shiwei = int(n % 100 / 10)
# 分解出个位的数字
gewei = n % 10
# 获得结果
jieguo = shiwei * gewei
# 输出结果
print(f'{shiwei} * {gewei} = {jieguo}')
# 将结果重新赋值给函数,再次循环、判断
n = jieguo
print(f'一共相乘了{count}次')
# 调用函数
number(98)
输出结果:
9 * 8 = 72
7 * 2 = 14
1 * 4 = 4
一共相乘了3次
def persistence(n: int) -> int:
'''
给定一个正整数n,请编写一个函数,将组成n的每个数字操作相乘,直到最终乘数结果是个位数为止。返回相乘操作的总次数。
示例:
输入:39,输出:3。因为39=27,27=14,1*4=4。
:param n:
:return:
'''
count = 0
while True:
# 将整数转换成字符串,再将每个数字拆开,组成列表;再对列表做遍历,转换成int类型相乘;将相乘后的结果再赋给n
num_str = str(n)
num = "_".join(num_str).split("_")
multiply = 1
for i in num:
multiply *= int(i)
n = multiply
count += 1
# 直到n为个数(<10)时,跳出循环
if n < 10:
break
return count
assert persistence(39) == 3
assert persistence(999) == 4
assert persistence(8) == 1
def persistence(n: int) -> int:
if len(str(n)) == 1:
return 1
count = 0
while len(str(n)) > 1:
result = 1
for i in str(n):
result *= int(i)
n = result
count += 1
return count
assert persistence(39) == 3
assert persistence(999) == 4
assert persistence(8) == 1
def persistence(n: int) -> int:
def mul(n):
result = 1
L = [int(i) for i in list(str(n))]
for i in L:
result *= i
return result
count =0
while(1):
n = mul(n)
count += 1
if n <10:
break
return count
assert persistence(39) == 3
assert persistence(999) == 4
assert persistence(8) == 1
def persistence(n: int) -> int:
if len(str(n)) == 1:
return 1
else:
count = 0
while True:
r = 1
for i in str(n):
r = r * int(i)
count += 1
if len(str(r)) == 1:
return count
else:
n = r
assert persistence(39) == 3
assert persistence(999) == 4
assert persistence(8) == 1
from functools import reduce
def persistence(n: int) -> int:
def get_result(num):
return reduce(lambda x,y:x*y,[int(i) for i in str(num)])
result = get_result(n)
cal = 1
while result>9:
result = get_result(result)
cal +=1
return cal