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)
Project Euler:Problem 93 Arithmetic expressions的更多相关文章
- Project Euler:Problem 55 Lychrel numbers
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...
- Project Euler:Problem 63 Powerful digit counts
The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is ...
- Project Euler:Problem 86 Cuboid route
A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, sits in the o ...
- Project Euler:Problem 76 Counting summations
It is possible to write five as a sum in exactly six different ways: 4 + 1 3 + 2 3 + 1 + 1 2 + 2 + 1 ...
- Project Euler:Problem 87 Prime power triples
The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is ...
- Project Euler:Problem 89 Roman numerals
For a number written in Roman numerals to be considered valid there are basic rules which must be fo ...
- Project Euler:Problem 39 Integer right triangles
If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exact ...
- Project Euler:Problem 28 Number spiral diagonals
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is forme ...
- Project Euler:Problem 47 Distinct primes factors
The first two consecutive numbers to have two distinct prime factors are: 14 = 2 × 7 15 = 3 × 5 The ...
随机推荐
- PC端 java 开发蓝牙所遇到的问题
由于项目的原因.要在电脑上开发一个通过蓝牙传送数据的client.我採用的是JAVA,JSME开发. client:去搜素蓝牙信号,然后找到对应的蓝牙信号进行连接. 服务端:client须要进行连接的 ...
- c3p0在spring中的配置
在大家的开发和学习其中应该经经常使用到数据库的连接和使用,只是连接 的方式就有非常多种方式了,例如说用最最简单的JDBC 也好,还实用比 较复杂一点的就是数据库连接池.当然还有使用DBCP的连接的,各 ...
- HDU 5616 Jam's balance 背包DP
Jam's balance Problem Description Jim has a balance and N weights. (1≤N≤20)The balance can only tell ...
- Linux下处理JSON的命令行工具:jq---安装
转自:https://blog.csdn.net/Sunny_much/article/details/50668871 JSON是前端编程经常用到的格式.Linux下也有处理处理JSON的 ...
- sicily 1342 开心的金明 (动规)
刷一下简单的背包问题 以下为代码: //1342. 开心的金明 #include <iostream> using namespace std; #define MAX(a,b) a> ...
- ie浏览器检测不到cookie的问题
之前做项目由于客户的要求设置缓存必须由后台来设置必须使用cookie(session是没问题的),后期设置时出现了登录页面与首页来还跳转的局面.原因就是首页没检测到登录后的缓存,而后台验证到确实已经是 ...
- [原创]微信小程序 实现 圆环 百分百效果
1.最终效果 2.技术点:a. css3 clip-path , b.根据角度和直边计算另一个直边的长度 3.实现思路: a.3个层(灰色圆形层, 红色圆形层,白色圆形层) ,其中灰色和红色层大小一 ...
- 如何去掉边框及input的兼容问题?
右偷个懒,发现别人写的也不错,我就做个小搬运工 如何去掉边框及input的兼容问题? 说到input,又不得不说它的兼容问题.input如何兼容各个浏览器呢? 第一步:清除input的border的默 ...
- (转载) Android开发时,那些相见恨晚的工具或网站!
huangmindong的专栏 目录视图 摘要视图 订阅 赠书 | 异步2周年,技术图书免费选 程序员8月书讯 项目管理+代码托管+文档协作,开发更流畅 Android ...
- Android官方培训课程中文版(v0.9.7)
Android官方培训课程中文版(v0.9.7) Google Android团队在2012年的时候开设了Android Training板块 - http://developer.android.c ...