思路:(1)根据输入格式可知最多可以到13!但是13!会达到最大,所以采用达到12。
(2)因为n^(1/2) * (n^(1/2)+1) 会大于我们输入的正整数,所以我们只需要遍历到n^(1/2),并且题目要求是不要求1的所以我们从2开始遍历。
(3)在实际考虑中我们需要分成质数和非质数。如果是质数,会直接输出我们的输入的正整数。
(4)我们采用取余数的方法判断是否为质数即 num % 2 == 0。
(5)符合我们将2存入列表中,2+1,继续判断num % 3 == 0,若符合继续存入。
(6)最长因子个数,如果是质数,直接输出1,不是由templen 判断
import math #导入数学计算相关的包
maxLen = 0 #最长连续因子个数
resultList = []#连续因子
isPrimary = True #标识是否为质数
num = int(input())
for i in range(2,int(math.sqrt(num))+1):#从2开始依次遍历至最大因子
tempNum = num
tempList=[]
start = i
while tempNum%start==0:#一轮遍历
isPrimary=False
tempNum = tempNum//start
tempList.append(str(start))
start+=1
tempLen=start-i #算出长度
if tempLen > maxLen :#更新数据
resultList=tempList
maxLen=tempLen
if isPrimary == True:#输出结果
print(1)
print(num)
else:
print(maxLen)
print("*".join(resultList))
代码及思路参考于:https://blog.csdn.net/qq_41912858/article/details/119464647?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164173274616780274196339%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164173274616780274196339&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-5-119464647.first_rank_v2_pc_rank_v29&utm_term=python%E8%BF%9E%E7%BB%AD%E5%9B%A0%E5%AD%90&spm=1018.2226.3001.4187