设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 4898|回复: 0
打印 上一主题 下一主题

【每周一坑】求乘积最大

[复制链接]

1

主题

0

好友

7

积分

新手上路

Rank: 1

跳转到指定楼层
楼主
发表于 2017-4-29 12:05:25 |显示全部楼层 |倒序浏览
题目地址:
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

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-3 13:51 , Processed in 0.024821 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部