Project Euler 93:Arithmetic expressions 算术表达式

时间:2021-08-10 02:15:25

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. &lbrack;project euler&rsqb; program 4

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

  4. 利用栈实现算术表达式求值&lpar;Java语言描述&rpar;

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

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

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

  6. 【算法】E&period;W&period;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. Switch&NAT 测试

    测试环境: PC1:Windows10 iperf3 PC2:Ubuntu iperf3 都装有千兆网卡,直连的iperf速度是935Mbps. 因为TXRX两个方向的数据是差不多的,下面的测试数据只 ...

  2. Windows Phone 三、样式和资源

    定义样式和引用资源 <Page.Resources> <!-- 向资源字典中添加一个键为ButtonBackground值为SolidColorBrush对象 --> < ...

  3. 解决IE9下JQuery的ajax失效的问题

    jquery ajax在跨域访问post请求的时候,ie9以下无效(包括ie9)   1. 设置浏览器安全属性,启用[通过域访问数据源]选项,如图:

  4. java程序开发代写&lpar;QQ&colon;928900200&rpar;

    条件:手机1.2都是安卓智能机,手机1开热点,手机2链接手机1,功能:A手机2通过刷手机网页,登陆手机1设定的页面并下载其手机的指定文件,B手机1控制手机2的流量,当通过的流量多的时候,停止流量的供应

  5. Alpha阶段小结

    1 团队的源码仓库地址 https://github.com/WHUSE2017/MyGod 2 Alpha过程回顾 2.1 团队项目预期 有一个可视化的安卓APP,实现二手交易基本功能.预期的典型用 ...

  6. lr使用linux Generator测试https莫名报 SSL protocol error when attempting to connect with host

    接收一个性能测试任务,各种原因需要使用linux agent产生压力.诡异的事发生了,同样脚本windows回放成功,使用linux agent报如下错误,脚本回放失败. Action.c(33): ...

  7. Maven手动安装jar包到仓库

    mvn install:install-file -Dfile=C:\Users\Administrator\Desktop\IKAnalyzer6.5.0.jar -DgroupId=com.luc ...

  8. PyQt5速成教程

    博客地址 https://www.jianshu.com/nb/26159952

  9. 36、XmlReader与 XMLWriter(抽象类)

    一.概述 XMLReader为抽象类,其派生类有:XmlDictionaryReader.XmlNodeReader.XmlTextReader(与IO命名空间中的TextReader对象一起使用). ...

  10. 使用Metasploit收集邮箱信息

    Metasploit提供了很多辅助的模块,非常实用.今天介绍一个叫search_email_collector的模块,它的功能是查找搜索引擎(google.bing.yahoo),收集和某个域名有关的 ...