[英]Using specific numbers to reach a target number
For my assignment, I have to allow the player to select 6 numbers from two different lists as they please. 对于我的任务,我必须允许玩家根据需要从两个不同的列表中选择6个数字。
List<Integer> large = Arrays.asList(25, 50, 75, 100);
List<Integer> small = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
7, 7, 8, 8, 9, 9, 10, 10);
After they have selected their numbers say [100, 3, 5, 6, 9, 5] they then generate a target number of lets say for example 299 and they then can only use the numbers selected as a means of reaching the target using ONLY multiplication, addition, subtraction and division. 他们选择了数字后说[100、3、5、6、9、5],然后生成目标数字,例如299,然后他们只能使用选择的数字作为仅使用目标的一种方式乘法,加法,减法和除法。 So they could input for instance, 100 * 3 + 5 - 6 to reach the 299 target and this would be checked and scored appropriately. 因此,他们可以输入100 * 3 + 5-6来达到299目标,并将对此进行适当的检查和评分。
Unfortunately I don't really have much to go on and I'm a bit confused on how to go about even starting to do that, I'm not looking for a straight up answer, maybe some pointers or external help would be much appreciated. 不幸的是我真的没有什么可做的,即使开始这样做我也很困惑,我不是在寻找一个直接的答案,也许一些指针或外部帮助将不胜感激。
Is this helpful? 这有帮助吗?
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class JavaApplication97 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] s = {1, 2, 3, 4};
List<Integer> large = new ArrayList<>(Arrays.asList(25, 50, 75, 100));
List<Integer> small = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10));
List<Integer> yourNumbers = new ArrayList<>();
int numbersToSelect = 6;
while (numbersToSelect > 0) {
System.out.println("Choose " + numbersToSelect + " numbers from these numbers : " + large + " or " + small);
Integer input = in.nextInt();
boolean isItThere = false;
if (large.contains(input)) {
isItThere = true;
large.remove(input);
} else if (small.contains(input)) {
isItThere = true;
small.remove(input);
}
if (isItThere) {
yourNumbers.add(input);
numbersToSelect--;
System.out.println("Number " + input + " is added");
} else {
System.out.println("There is no such number");
}
}
}
}
Sample output : 样本输出:
Choose 6 numbers from these numbers : [25, 50, 75, 100] or [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
25
Number 25 is added
Choose 5 numbers from these numbers : [50, 75, 100] or [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
25
There is no such number
Choose 5 numbers from these numbers : [50, 75, 100] or [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
5
Number 5 is added
Choose 4 numbers from these numbers : [50, 75, 100] or [1, 1, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
5
Number 5 is added
Choose 3 numbers from these numbers : [50, 75, 100] or [1, 1, 2, 2, 3, 3, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
5
There is no such number
Choose 3 numbers from these numbers : [50, 75, 100] or [1, 1, 2, 2, 3, 3, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
100
Number 100 is added
Choose 2 numbers from these numbers : [50, 75] or [1, 1, 2, 2, 3, 3, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
6
Number 6 is added
Choose 1 numbers from these numbers : [50, 75] or [1, 1, 2, 2, 3, 3, 4, 4, 6, 7, 7, 8, 8, 9, 9, 10, 10]
50
Number 50 is added
If we follow Bedmas (brackets exponents division multiplication addition subtraction) we can break this down into a simple function. 如果遵循Bedmas(方括号指数除法乘除法),我们可以将其分解为一个简单的函数。
First turn the equation into a list of components: 首先将方程式转换为组件列表:
100 * 3 + 5 - 6
changes to 更改为
["100", "*", "3", "+", "5", "-", "6"]
Now evaluate every element to make sure that they are valid. 现在评估每个元素以确保它们有效。 ie) Each value in the list of components must be in the selection list or have the value, */+-, Also if there are n nums, then there should be n-1 syms ie)组件列表中的每个值都必须在选择列表中或具有值* / +-。此外,如果有n个数字,则应该有n-1个符号
To get the result we can then evaluate the list,.. merging num-sym-num sections as we go, in the order of bedmas 为了获得结果,我们可以然后评估列表,..按照睡床的顺序合并num-sym-num节
In pseudo: 伪:
func int compute_val(ListString eqn)
while not eqn.length is 1
if "*" in eqn
index = eqn.getIndex("*")
replace eqn[index -1:index +1] with str((int) eqn[index -1] * (int)eqn[index +1])
else if "/" in eqn
index = eqn.getIndex("/")
replace eqn[index -1:index +1] with str((int) eqn[index -1] / (int)eqn[index +1])
else if "+" in eqn
index = eqn.getIndex("+")
replace eqn[index -1:index +1] with str((int) eqn[index -1] + (int)eqn[index +1])
else if "-" in eqn
index = eqn.getIndex("-")
replace eqn[index -1:index +1] with str((int) eqn[index -1] - (int)eqn[index +1])
return (int)eqn[0]
This would be the progression of the list as the equation is evaluated in the loop 这是列表的进度,因为在循环中评估方程式
["100", "*", "3", "+", "5", "-", "6"] --> ["300", "+", "5", "-", "6"] -->
["305", "-", "6"] --> ["299"]
You need multiple steps, and as I guess this is a homework task I'm not going to give a complete answer, however: 您需要执行多个步骤,而且我想这是一项家庭作业,因此我不会给出完整的答案:
While 2 and 3 are trivial, the first part is probably the hardest for you and the core of the task. 虽然2和3无关紧要,但第一部分可能对您来说是最难的,也是任务的核心。 You can get more information on that task here: Smart design of a math parser? 您可以在此处获得有关该任务的更多信息: 数学解析器的智能设计?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.