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 ...
随机推荐
- web前端项目规范
项目目录规范 . ├─ css ├─ component ├─ img ├─ js ├─ page ├─ test ├─ package.json ├─ README.md css 存放样式类文件,且 ...
- 洛谷 P3912 素数个数
P3912 素数个数 题目描述 求1,2,\cdots,N1,2,⋯,N 中素数的个数. 输入输出格式 输入格式: 1 个整数NN. 输出格式: 1 个整数,表示素数的个数. 输入输出样例 输入样例# ...
- 为什么选性别会导致兴趣都选中-vue
为什么选性别会导致兴趣都选中-vue <%@ page language="java" import="java.util.*" pageEncoding ...
- Android程序之全国天气预报查询(聚合数据开发)
一.项目演示效果例如以下: 项目源码下载地址: http://pan.baidu.com/s/1pL6o5Mb password:5myq 二.使用 聚合数据SDK: (1)聚合数据官网地址:http ...
- JDBC创建mysql连接池代码
1.底层实现类(DBConnection) package JDBC.JDBCPool.MyJDBCPool; import java.sql.Connection; import java.sql. ...
- nodejs即时聊天
一直想做一个即时聊天的应用,前几天看到了socket.io,感觉还不错.自己略加改动,感觉挺不错的.官网上给的样例非常easy,以下改进了一点,实现了历史消息的推送. demo地址:chat.code ...
- 基于One-Class的矩阵分解方法
在矩阵分解中. 有类问题比較常见,即矩阵的元素仅仅有0和1. 相应实际应用中的场景是:用户对新闻的点击情况,对某些物品的购买情况等. 基于graphchi里面的矩阵分解结果不太理想.调研了下相关的文献 ...
- vuex3
以下是一个表示“单向数据流”理念的极简示意: 但是,当我们的应用遇到多个组件共享状态时,单向数据流的简洁性很容易被破坏: 多个视图依赖于同一状态. 来自不同视图的行为需要变更同一状态. 这就是 Vue ...
- 什么是域名的TTL值? ——一条域名解析记录在DNS缓存服务器中的存留时间
什么是域名的TTL值? 转自:http://hizip.net/index.php/archives/20/TTL(Time-To-Live),就是一条域名解析记录在DNS服务器中的存留时间.当各地的 ...
- 安卓-活动Activity
Android有4大组件,活动 Activity,服务 Service ,广播接收器 Brostcast receiver,内容提供器 Content Provider 安卓活动的生命周期有7种, o ...