下面是代码 #coding=utf-8 '''随机输入一个任意长度的数字,把其分割两部分,算出积成最大''' def product(num):
num_str = num
result_max = 0 for i in range(1,len(num_str)):
num_left = int(num_str[:i])
right = i-len(num_str)
num_right = int(num_str[right:])
result = num_left*num_right if result > result_max:
result_max = result
result_left = num_left
result_right = num_right print result_left,result_right,result_max
'''随机输入一个任意长度的数字,随机排列后,把其分割两部分,算出积成最大'''import itertools def product_2(num):
num_str = num
num_li =[]
result_max = 0
iter = itertools.permutations(num_str,len(num_str)) #输入的数字需要排列组合
li1 = list(iter) for i in range(0,len(li1)):
li2 = li1
num_str2 = '' for j in range(0,len(num_str)):
num_str2 += li2[j]
num_str = num_str2
num_li.append(num_str2) while len(num_li) > 0:
num_str = num_li.pop() for i in range(1,len(num_str)):
num_left = int(num_str[:i])
right = i-len(num_str)
num_right = int(num_str[right:])
result = num_left*num_right if result > result_max:
result_max = result
result_left = num_left
result_right = num_right print result_left,result_right,result_max
def separateNum(number): # 将所有排序的数字的分割,然后找到最大值
listt = []
listMax = []
for j in number:
for i in range(1, len(j)):
intA = int(j[:i])
intB = int(j[i:])
listt.append(intB * intA)
listMax.append(max(listt))
print max(listMax)
def permutationMethod(sourceNum): # 输入一个数字,将其所有的排列装进
iter = itertools.permutations(sourceNum, len(sourceNum))
listA = list(iter)
listB = []
for i in listA: # listA的元素是tuple类型的
strAll = ""
for j in i:
strAll += j
listB.append(strAll)
separateNum(listB)
listA = []
isTrue = True
while (isTrue):
input = raw_input("请输入一个大于等于10数字:")
if input.isdigit():
if input.__len__() <= 1:
print "您输入的数字小于10,请重新输入"
else:
isTrue = False
permutationMethod(input)
else:
print "请输入一个纯数字"