• Project Euler:Problem 93 Arithmetic expressions


    By using each of the digits from the set, {1, 2, 3, 4}, exactly once, and making use of the four arithmetic operations (+, −, *, /) and brackets/parentheses, it is possible to form different positive integer targets.

    For example,

    8 = (4 * (1 + 3)) / 2
    14 = 4 * (3 + 1 / 2)
    19 = 4 * (2 + 3) − 1
    36 = 3 * 4 * (2 + 1)

    Note that concatenations of the digits, like 12 + 34, are not allowed.

    Using the set, {1, 2, 3, 4}, it is possible to obtain thirty-one different target numbers of which 36 is the maximum, and each of the numbers 1 to 28 can be obtained before encountering the first non-expressible number.

    Find the set of four distinct digits, a < b < c < d, for which the longest set of consecutive positive integers, 1 to n, can be obtained, giving your answer as a string: abcd.



    先求出10选4的全部组合情况,保存为list

    对于每一种组合都有24种排列情况

    每个排列情况其运算顺序都有5种

    关于四个数的运算涉及到3个操作符。并且每一个操作符理论上有四种选择:加减乘除。并将得出的整数运算结果标记出来。


    终于是要比較每一种组合的标记出来的结果,从1到n都有标记的最大的那个n


    def xcombination(seq,length):
            if not length:
                    yield []
            else:
                    for i in range(len(seq)):
                            for result in xcombination(seq[i+1:],length-1):
                                    yield [seq[i]]+result
    
    def nextPermutation(self, num):
            if len(num) < 2:
                return num
            partition = -1
            for i in range(len(num) - 2, -1, -1):
                if num[i] < num[i + 1]:
                    partition = i
                    break
            if partition == -1:
                return num[::-1]
            for i in range(len(num) - 1, partition, -1):
                if num[i] > num[partition]:
                    num[i], num[partition] = num[partition], num[i]
                    break
            num[partition + 1:] = num[partition + 1:][::-1]
            return num
    
    
    def ope(a,b,num):
        if a==None or b==None:
            return None
        if num == 1:
            return a+b
        if num == 2:
            return a-b
        if num == 3:
            return a*b
        if num == 4:
            if b == 0:
                return None
            else:
                return a/b
    
    
    comb=xcombination([i for i in range(10)],4)
    comb_list=list(comb)
    bestprem=[0 for i in range(4)]
    bestres=0
    for prem in comb_list:
        tmp=prem
        flag=1
        num_list=[0]*(9*8*7*6)
        while tmp != prem or flag==1:
            flag=0
            for i in range(1,5):
                for j in range(1,5):
                    for k in range(1,5):
                        num=ope(ope(ope(prem[0],prem[1],i),prem[2],j),prem[3],k)
                        if num!=None and num==int(num) and num > 0 and num < len(num_list):
                            num_list[int(num)]=True
    
                        num=ope(ope(prem[0],ope(prem[1],prem[2],j),i),prem[3],k)
                        if num!=None and num==int(num) and num > 0 and num < len(num_list):
                            num_list[int(num)]=True
    
                        num=ope(prem[0],ope(ope(prem[1],prem[2],j),prem[3],k),i)
                        if num!=None and num==int(num) and num > 0 and num < len(num_list):
                            num_list[int(num)]=True
    
                        num=ope(prem[0],ope(prem[1],ope(prem[2],prem[3],k),j),i)
                        if num!=None and num==int(num) and num > 0 and num < len(num_list):
                            num_list[int(num)]=True
    
                        num=ope(ope(prem[0],prem[1],i),ope(prem[2],prem[3],k),j)
                        if num!=None and num==int(num) and num > 0 and num < len(num_list):
                            num_list[int(num)]=True
            count=1
            while num_list[count]==True:
                count=count+1
    
            if count > bestres:
                bestres=count
                bestprem=prem
    
            prem=nextPermutation((),[prem[i] for i in range(4)])
            
    print(bestres,' ',bestprem)
        
            
    



  • 相关阅读:
    第一阶段大作业 文件上传格式
    第一阶段大作业 数据字典的修改
    设计模式 C++实现职责链模式 (顺便复习C++)
    Numpy学习
    2019版:第二章:(1)Redis 概述
    第一章:(6)Dubbo 与 SpringBoot 整合
    第一章:(5)Dubbo 监控中心
    2019版:第一章:(2)NOSQL 数据库
    2019版:第二章:(3)Redis 其他相关知识
    2019版:第一章:(1)技术发展
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7059184.html
Copyright © 2020-2023  润新知