- 帖子
- 1
- 精华
- 0
- 积分
- 7
- 阅读权限
- 10
- 注册时间
- 2016-2-16
- 最后登录
- 2017-4-29
|
题目地址:
https://mp.weixin.qq.com/s?__biz ... LPUZz7%2BHBPAW1q#rd
下面是代码
#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
|
|