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 ...
随机推荐
- VUE:路由
VUE:路由 一.说明 1)官方提供的用来实现SPA的vue插件 2)github:https://github.com/vuejs/vue-router 3)中文文档:http://router.v ...
- 前端的标配:npm是什么及其安装(含cnpm)
前端的标配:npm是什么及其安装 一:npm是什么及其来源 参考来源:npm是干什么的 总结:不需要去相关的网站下载依赖,用一个工具把这些依赖集中起来管理 NPM 的思路大概是这样的: 1)买个服务器 ...
- poi操作excel2007(读取、生成、编辑)
因为现在再写excel2003版的比较low,所以我在这就不介绍了,直接介绍2007,我所用的编程软件是IDEA poi操作office总共有6个jar包,在pom.xml文件中配置如下,也可下载后直 ...
- 【HDU 6299】Balanced Sequence
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 我们贪心地把每一个括号序列能匹配都按照栈的规则都匹配出来. (直接递增匹配对数*2就可以了 最后栈里面就只剩下类似))))((((( ...
- Opencv 使用Rect选取与设置窗口ROI
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50593825 首先看一下Rect对象的 ...
- Android程序之全国天气预报查询(聚合数据开发)
一.项目演示效果例如以下: 项目源码下载地址: http://pan.baidu.com/s/1pL6o5Mb password:5myq 二.使用 聚合数据SDK: (1)聚合数据官网地址:http ...
- OPENWRT中SSH免密钥登陆(具体步骤)
通过使用ssh-keygen生成公钥,在两台机器之间互相建立新人通道极客. 如果本地机器是client,远程机器为server. 1.使用ssh-keygen生成rsa keygen(在这里会覆盖曾经 ...
- inheritance in kentico
Visual inheritance http://devnet.kentico.com/docs/7_0/devguide/index.html?visual_inheritance.htm The ...
- legend---十一、thinkphp事务中if($ans1&&$ans2){}else{}方式和try{}catch{}方式事务操作的区别在哪里
legend---十一.thinkphp事务中if($ans1&&$ans2){}else{}方式和try{}catch{}方式事务操作的区别在哪里 一.总结 一句话总结:执行的条件其 ...
- ElasticSearch 深入理解 三:集群部署设计
ElasticSearch 深入理解 三:集群部署设计 ElasticSearch从名字中也可以知道,它的Elastic跟Search是同等重要的,甚至以Elastic为主要导向. Elastic即可 ...