用0、1、2、3、4、5、6、7、8、9 十个数字摆出两个五位数,使这两个五位数相加和最大,可以摆出怎样的算式?相加和是多少?

#!/usr/bin/env
# -*- coding:utf-8 -*-
import time

if __name__ == "__main__":
    a = b = c = d = e = 0  # 每一位的值,a是万位上,b是千位,c是百位,d是十位,e是个位
    h = n = 0  # 记录总共拼接数字个数
    number_array = []  # 记录所拼接出的五位不重复数字

    for a in range(0, 10):  # 万位的可能性
        for b in range(0, 10):  # 千位的可能性
            for c in range(0, 10):  # 百位的可能性
                for d in range(0, 10):  # 十位的可能性
                    for e in range(0, 10):  # 个位的可能性
                        if a == b or a == c or a == d or a == d or b == c or b == d or b == e or c == d or c == e or d == e or a == 0:  # 数字只能使用一次,首位不能为0
                            h = h + 1
                        else:
                            number = str("%d%d%d%d%d" % (a, b, c, d, e))
                            number_array.append(number)
                            n = n + 1

    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) + "功能拼接出%d个数值" % n)

    answer = 0  # 两个值加和的最大值
    answer_number = []  # 那两个数字

    # 在所有拼接出来的5位数中两个值相加取最大的和
    for i in number_array:
        for j in number_array:
            if i != j:
                if (int(i) + int(j)) >= int(answer):
                    answer = int(i) + int(j)
                    print(answer)
                    answer_number.append({i, j})

    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) + str(answer))
    print(answer_number)
Last modification:April 7th, 2020 at 12:05 am
如果觉得我的文章对你有用,请随意赞赏