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. 《Java核心技术与最佳实践》读书笔记

    第一章 Java7新语法 1.switch中使用字符串 2.增加二进制表示0b10101010:数字字面量允许直径使用下划线12_34_90 3.一个catch字句捕获多个异常,多个异常之间用|分隔 ...

  2. ASP.NET MVC 表单的几种提交方式

    下面是总结一下在ASP.NET MVC中表单的几种提交方式. 1.Ajax提交表单 需要引用 <script type="text/javascript" src=" ...

  3. 重写DataGridViewColumn

    做个项目需要用到DataGridView,这个控件还是挺好用的,但是今天却发现无法实现自己想要的功能.主要是DataGridViewCheckBoxColumn这个列虽然是提供了复选框,但是却未能在复 ...

  4. gulp插件

    gulp是趋势 gulp完全开发指南 => 快来换掉你的Grunt吧 gulp的工作流程:文件流--文件流--文件流......因为grunt操作会创建临时文件,会有频繁的IO操作,而gulp使 ...

  5. java 高精度

    package BigDecimal; import java.math.BigDecimal; import java.lang.Object; public class BigDecimalTes ...

  6. ASP.NET MVC4学习笔记之总体概述

    断断续续使用ASP.NET MVC框架也有一年多了,也算积累了一些经验,唉,一直想写一些笔记好好总结一下,人太懒不想动笔,今天终于决定开始.希望自己能坚持下去. 这篇文章大体介绍ASP.NET MVC ...

  7. 原创的基于HTML/CSS/JavaScript的层级目录树

    之前参加过一些基于HTML/CSS/JavaScript的项目,当在页面中需要生成一颗目录树时,总是首先想着网上有没有现成的生成树的源代码,比如dtree.zthee,或者使用一些javascript ...

  8. 一个订单相关的存储过程(MySQL)

    BEGIN DECLARE currentDate VARCHAR(15) ;/*当前日期,有可能包含时分秒 */ DECLARE maxNo INT DEFAULT 0 ; /* 离现在最近的满足条 ...

  9. WPF-控件-将ListBox条目水平排列

    <Grid Margin="6"> <ListBox> <!--ItemsPanel--> <ListBox.ItemsPanel> ...

  10. Win7任务计划自由预设系统定时自动关机

    大家在使用电脑的时候可能会遇到一些需要无人值守让电脑自行执行任务后定时关机的情形,在Win7系统中,我们可以使用"任务计划"设置功能结合shutdown命令灵活设置任务计划,让Wi ...