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 ...
随机推荐
- windows下的ubuntu
办公用Windows确实方便,但对于开发和学习还是用Linux比较好. 在Windows下安装Linux子系统 windows10中推出了Linux子系统,这个功能对开发和学习来说真的很好,非常方便. ...
- 上机题目(0基础)-计算两个正整数的最大公约数和最小公倍数(Java)
题目例如以下:
- 【MFC设置静态文本框背景为透明】
视图类中加入OnCtlColor()函数: IDC_STATIC1为静态文本框ID HBRUSH CAngleView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT n ...
- 任务调度器quartz的使用
1.quartz的获取. 可參照:Quartz任务调度模型实例 2.开发思路: 要使用定时器quartz.先弄清楚三个概念:调度器.任务.触发器.开发也是依照这三个方面来开发, 1>写一个Job ...
- 使用ViewPager实现广告滑动效果
效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSk1DNjAx/font/5a6L5L2T/fontsize/400/ ...
- RedHat6.5 安装OpenStack all in one-RDO方式
OpenStack是一个开源的云平台.由各个组件协同工作,安装非常复杂. RedHat有个关于Openstack的项目RDO,能够简化安装过程,可是假设真按RDO所说的三步去安装,发现安装过程中还是有 ...
- ThinkPHP5.0框架开发--第1章 Tp5.0安装
ThinkPHP5.0框架开发--第1章 Tp5.0安装 第1章 Tp5.0 安装 ======================================================== 今 ...
- composer是什么
composer是什么 Composer 是 PHP5.3以上 的一个依赖管理工具.它允许你声明项目所依赖的代码库,它会在你的项目中为你安装他们.Composer 不是一个包管理器.是的,它涉及 &q ...
- ES task管理
Task Management API The Task Management API is new and should still be considered a beta feature. Th ...
- Oracle 数据泵使用详解--精华版
数据泵使用EXPDP和IMPDP时应该注意的事项: EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端 ...