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.


算术表达式

使用集合{1, 2, 3, 4}中每个数字恰好一次以及(+, −, *, /)四则运算和括号,可以得到不同的正整数。

例如,

8 = (4 * (1 + 3)) / 2
14 = 4 * (3 + 1 / 2)
19 = 4 * (2 + 3) − 1
36 = 3 * 4 * (2 + 1)

注意不允许直接把数字连起来,如12 + 34。

使用集合{1, 2, 3, 4},可以得到31个不同的数,其中最大值是36,以及1到28之间所有的数。

若使用包含有四个不同数字a < b < c < d的集合可以得到从1到n之间所有的数,求其中使得n最大的集合,并将你的答案写成字符串:abcd。

解题

表示感觉不知道如何下手!!!???

在先找打答案,然后看论坛中的讲解,找到一个很笨的方法。

1.先找出运算符和括号的所有匹配表达式

2.这里的表达式是中序表达式,转换成后续表达式,在转换成后序表达式的过程中去除括号

3.后序表达式是最用于求解的了,对每个表达式,求解

4.对固定的a b c d 求解后,需要判断解是否是1到n的值,若是,则记录最大值n

5.对所有的a b c d 找出最大的n

上面的计算结果都是负的。。。不明白

下面看到论坛中Python实现的,程序中进行了注释,感觉就是运气得出的答案。

# coding=gbk

import time as time
from itertools import permutations
from itertools import combinations
from itertools import product
# 解题思路:
# 对0-9内人去4个数
# 四种运算任意组合
# 对取得4个数和四种运算组合的一种进行计算,
## 取得4个数再任意排列,和唯一的运算符组合进行计算
# 但是为什么不考虑括号的问题,不加括号也算了,为什么四则运算的先后顺序也变了,我表示很费解。但是结果还对了
# 四个运算符取三个,就是运算符的所以可能
ops_set = list(product("+-*/","+-*/","+-*/")) print 0/3
print 12/3
print 13/3
l = [1,2,3,4]
for i in range(4):
print l.pop()
def calc(ls,ops):
l = [float(i) for i in ls ] t = l.pop()
for op in ops:
if op== '+':
t+= l.pop()
elif op=='-':
t -=l.pop()
elif op=='*':
t *=l.pop()
else:
k=l.pop()
if k==0:
return 0
t/=k
print ls ,ops ,t
if int(t) == t:
return abs(int(t)) return 0 def getlongest(ls):
l = list(permutations(ls,4))
# print l
s = sorted(list(set([calc(ll,ops) for ops in ops_set for ll in l])))
last_n = 0
for n in s:
if n==0:continue
if n == last_n+1:
last_n = n
continue
break
return last_n def run():
maxx=[0]
for l in permutations(range(10),4):
# print l
x = getlongest(l)
if x>maxx[0]:
maxx = [x,l]
print maxx t0 = time.time()
run()
t1 = time.time()
print "running time=",(t1-t0),"s"

表示现在还无法理解,以后继续完善

Project Euler 93:Arithmetic expressions 算术表达式的更多相关文章

  1. 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 ari ...

  2. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  3. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  4. 利用栈实现算术表达式求值(Java语言描述)

    利用栈实现算术表达式求值(Java语言描述) 算术表达式求值是栈的典型应用,自己写栈,实现Java栈算术表达式求值,涉及栈,编译原理方面的知识.声明:部分代码参考自茫茫大海的专栏. 链栈的实现: pa ...

  5. OpenJudge计算概论-简单算术表达式求值

    /*===================================== 简单算术表达式求值 总时间限制: 1000ms 内存限制: 65536kB 描述 2位正整数的简单算术运算(只考虑整数运 ...

  6. 【算法】E.W.Dijkstra算术表达式求值

    算术表达式求值 我们要学习的一个栈的用例同时也是展示泛型的应用的一个经典例子,就是用来计算算术表达式的值,例如 ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) ) 如果将4乘以5,把3 ...

  7. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  8. page80-栈用例-算术表达式求值

    表达式由括号, 运算符和操作数(数字)组成.我们根据以下4中情况从左到右逐个将这些实体送入栈处理. (1)将操作数压入操作数栈: (2)将运算符压入运算符栈: (3)忽略左括号: (4)在遇到右括号时 ...

  9. SDUT2484算术表达式的转换

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2484&cid=1182 题目描述 小明在学习了数据结构之后,突然想起了以前没有解决的算术 ...

随机推荐

  1. JavaScript 内部对象

    欢迎访问我的个人博客:http://blog.cdswyda.com/ 原文文地址:JavaScript 内部对象

  2. 设置Linux时间 同步时间

    date命令将日期设置为2014年6月18日 ----   date -s 06/18/14 将时间设置为14点20分50秒 ----   date -s 14:20:50 将时间设置为2014年6月 ...

  3. Python开发【第一篇】Python模块中特殊变量

    模块中特殊变量 生产环境中,常用的就是__name__和__file__ __doc__ __package__ __cached__ __name__ __file__ 一. __doc__  #获 ...

  4. 正确处理WPF中Slider值改变事件的方式

    最近在用WPF数据绑定重写一下播放器项目时遇到的关于Slider的问题,在窗体透明度调节和播放进度调节上用了Slider控件.调节窗体透明度我是 这么想的:将窗体的Opacity属性的值与Slider ...

  5. WCF-Configuration

    Host-Configuration <?xml version="1.0"?> <configuration> <configSections> ...

  6. 从零单排学JavaWeb

    之前是一个asp爱好者,感觉前途渺茫,特此转向Powerful的Java阵型,寻求心灵上的慰藉. 把自己遇到的问题记录下来,同时也分享给大家.  环境-下载 1 JDK http://dlsw.bai ...

  7. Oracle收缩表空间

    可以使用 alter database datafile 'file path...' resize xM 的命令来缩小数据文件. SELECT 'alter database datafile '' ...

  8. 使用ab测试工具 进行并发测试

    ab.exe -n1000 -c100 http://localhost:8067/api/todo/555e95feb301baa678141148 http://www.cnblogs.com/y ...

  9. Sql例子Sp_ExecuteSql 带参数

    Declare @i int, @projectCount int ) --参数 ) ) ) ) ) ) --循环变量起始 --得到所有的项目 select @projectCount = count ...

  10. sqlserver 动态表名 动态字段名 执行 动态sql

    动态语句基本语法: 1 :普通SQL语句可以用exec执行 Select * from tableName exec('select * from tableName') exec sp_execut ...