从集合中的数字计算目标数字 [英] Computing target number from numbers in a set

查看:24
本文介绍了从集合中的数字计算目标数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个家庭作业问题,它问我这个问题:

I'm working on a homework problem that asks me this:

给定一个有限的数字集合和一个目标数字,使用基本数学运算(add、sub、mult、div)和使用集合中的每个数字,找出该集合是否可用于计算目标数字恰好一次(所以我需要用尽这组).这必须通过递归来完成.

Tiven a finite set of numbers, and a target number, find if the set can be used to calculate the target number using basic math operations (add, sub, mult, div) and using each number in the set exactly once (so I need to exhaust the set). This has to be done with recursion.

所以,例如,如果我有一套

So, for example, if I have the set

{1, 2, 3, 4}

目标 10,然后我可以使用

and target 10, then I could get to it by using

((3 * 4) - 2)/1 = 10. 

我正在尝试用伪代码来表述算法,但到目前为止还没有走得太远.我认为图表是要走的路,但肯定会感谢您对此的帮助.谢谢.

I'm trying to phrase the algorithm in pseudo-code, but so far haven't gotten too far. I'm thinking graphs are the way to go, but would definitely appreciate help on this. thanks.

推荐答案

在考虑如何解决问题(比如用图表)之前,看看问题真的很有帮助.如果您发现自己陷入困境并且似乎无法提出任何伪代码,那么很可能是有什么阻碍了您;其他一些尚未解决的问题或疑虑.在这种情况下,一个棘手"问题的示例可能是这个问题的递归究竟是什么?"

Before thinking about how to solve the problem (like with graphs), it really helps to just look at the problem. If you find yourself stuck and can't seem to come up with any pseudo-code, then most likely there is something that is holding you back; Some other question or concern that hasn't been addressed yet. An example 'sticky' question in this case might be, "What exactly is recursive about this problem?"

在阅读下一段之前,请先尝试回答这个问题.如果你知道什么是递归问题,那么编写一个递归方法来解决它可能不是很困难.

Before you read the next paragraph, try to answer this question first. If you knew what was recursive about the problem, then writing a recursive method to solve it might not be very difficult.

您想知道某个使用一组数字(每个数字仅使用一次)的表达式是否为您提供了目标值.有四个二元运算,每个都有一个inverse.因此,换句话说,您想知道第一个数字是否与其他数字的某种表达式一起运算给了您目标.好吧,换句话说,您想知道其他"数字的某些表达是否是[...].如果没有,那么对第一个数字使用第一个操作并不能真正满足您的需求,因此请尝试其他操作.如果它们不起作用,那么也许它本来就不是.

You want to know if some expression that uses a set of numbers (each number used only once) gives you a target value. There are four binary operations, each with an inverse. So, in other words, you want to know if the first number operated with some expression of the other numbers gives you the target. Well, in other words, you want to know if some expression of the 'other' numbers is [...]. If not, then using the first operation with the first number doesn't really give you what you need, so try the other ops. If they don't work, then maybe it just wasn't meant to be.

我认为这是四个不带括号的运算符的中缀表达式,因为对原始问题的评论说括号是为了示例而添加的(为了清楚起见?)并且括号的使用没有明确声明.

I thought of this for an infix expression of four operators without parenthesis, since a comment on the original question said that parenthesis were added for the sake of an example (for clarity?) and the use of parenthesis was not explicitly stated.

这篇关于从集合中的数字计算目标数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆