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. Android SDK 更新失败

    万恶的墙,调查兵团赶紧把墙拆了.大家一起跟巨人打一架. 解决方法是改hosts文件 添加 74.125.237.1 dl-ssl.google.com ok,good job 多亏了http://bl ...

  2. Freemarker例子

    1.引入架包 2.写ftl文件 3.代码 hello.ftl 你好啊,${hello},今天你的精神不错! if else 语句测试 <#if num gt 18><#-- 不使用 ...

  3. 【面试虐菜】—— Apache知识整理

    Apache性能调优1 Apache 部分:1. 移除不用的模块.2. 使用 mod_disk_cache NOT mod_mem_cache .3. 扁平架构配置mod_disk_cache.4.  ...

  4. vs2010中安装ASP.NET AJAX Control Toolkit

    方法一: 第一步 下载Ajax Control Toolkit 进入网址http://ajaxcontroltoolkit.codeplex.com/ 即可下载 第二步 解压下载下来的Ajax Con ...

  5. require.js入门指南(三)

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  6. oracle中的记录类型

    单词RECORD有“记录”的意思,因此RECORD也称为“记录类型”,使用该类型的变量可以存储由多个列值组成的一行数据. 在声明记录类型变量之前,首先需要定义记录类型,然后才可以声明记录类型的变量. ...

  7. SVN四部曲之SVN使用详解进阶

    SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...

  8. [ios]ipad下的splitViewController 让你的APP看起来酷酷的!

    在ipad下可以使用splitViewController splitViewController下包含两个viewController 这是一种将屏幕一分为二的方式. 在水平状态下会出现成两个左右两 ...

  9. DBA应该知道的一些SQL Server跟踪标记

    跟踪标记是什么? 对于DBA来说,掌握Trace Flag是一个成为SQL Server高手的必要条件之一,在大多数情况下,Trace Flag只是一个剑走偏锋的奇招,不必要,但在很多情况下,会使用这 ...

  10. Inject js code to exchange 2013

    1. save the following code to C:\Program Files\Microsoft\Exchange Server\V15\FrontEnd\HttpProxy\owa ...