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)
    
        



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值