題目
給定兩個正整數 N
1
<N
2
。把從 N
1
到 N
2
的每個數的各位數的立方相乘,再將結果的各位數求和,得到一批新的數字,再對這批新的數字重複上述操作,直到所有數字都是 1 位數爲止。這時哪個數字最多,哪個就是“數字之王”。
例如 N
1
=1 和 N
2
=10 時,第一輪操作後得到 { 1, 8, 9, 10, 8, 9, 10, 8, 18, 0 };第二輪操作後得到 { 1, 8, 18, 0, 8, 18, 0, 8, 8, 0 };第三輪操作後得到 { 1, 8, 8, 0, 8, 8, 0, 8, 8, 0 }。所以數字之王就是 8。
本題就請你對任意給定的 N
1
<N
2
求出對應的數字之王。
輸入格式:
輸入在第一行中給出兩個正整數 0<N
1
<N
2
≤10
3
,其間以空格分隔。
輸出格式:
首先在一行中輸出數字之王的出現次數,隨後第二行輸出數字之王。例如對輸入 1 10 就應該在兩行中先後輸出 6 和 8。如果有並列的數字之王,則按遞增序輸出。數字間以 1 個空格分隔,行首尾不得有多餘空格。
輸入樣例:
10 14
結尾無空行
輸出樣例:
2
0 8
結尾無空行
解題思路
start,end = map(int,input().split())
# start,end = map(int,"10 14".split())
# start,end = map(int,"1 10".split())
inputList = [str(i) for i in range(start, end+1)]
def actionRes(a:str) -> str:
res = 1
for i in a:
res = res *(int(i)**3)
b = str(res)
resb = 0
for j in b:
resb += int(j)
return str(resb)
def sumLength(list:[str])->int:
res = 0
for i in list:
res += len(i)
return res
while sumLength(inputList) != len(inputList):
for index,val in enumerate(inputList):
inputList[index] = actionRes(val)
# print(inputList)
from collections import Counter
res = Counter(inputList).most_common()
if len(res) == 0:
print("")
else:
resOutput = [int(x) for x,y in res if y == res[0][1]]
resOutput.sort()
print(res[0][1])
resOutput = [str(x) for x in resOutput]
print(" ".join(resOutput))