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)
最新文章
- dedecms 采集规则过滤与替换
- Android Studio 快捷键一览
- 美帝的emal to message gateway
- 深入学习JavaScript(一)
- windos命令行下的程序编写
- IOC Container(服务容器)的工作机制
- NSString asscii格式(2进制) 转 utf8格式——解决iOS自己处理http socket数据,遇到Transfer-Encoding: chunked时
- java程序(一)----HashMap同时获取键值
- OPENSSL安装 以及使用openssl中的AES加密和解密
- UVALive 4959 Jumping monkey
- mysql+php+pdo批量添加大数据
- lua 中 IO库
- [SHOI 2011]双倍回文
- lua tonumber
- C++实现景区信息管理系统
- VMware Workstation 14.1.1 精简特别版
- Linux部署Web应用程序超链接下载中文名称文件404问题解决办法
- linux命令之vi文本编辑器
- Fortran编译器之一GUN Fortran安装(Windows XP)
- 实例直观解释sessionid的作用
热门文章
- 一行代码解决IE兼容性问题
- SELECT使用子查询
- PatentTips – Java native function calling
- 洛谷 P1617 爱与愁的一千个伤心的理由
- synchronized与static synchronized 的差别、synchronized在JVM底层的实现原理及Java多线程锁理解
- 编写html经常使用而又easy忘记的语句
- 数据库之Case When
- shareSDK的初步使用(shareSDK中微信、qq等兼容问题,以及cocoapods支持架构冲突问题的解决)
- Android 启动界面的制作
- bzoj3173: [Tjoi2013]最长上升子序列(树状数组+二分倒推)