简体   繁体   English

使用特定数字达到目标数字

[英]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: 您需要执行多个步骤,而且我想这是一项家庭作业,因此我不会给出完整的答案:

  1. Parse the input string of the user 解析用户的输入字符串
  2. Calculate the result 计算结果
  3. Check if the result equals the target number 检查结果是否等于目标数

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.

相关问题 以最少的步数从0达到目标数 - To reach the target number from 0 in minimum number of steps 5个数字可以通过加,减或乘运算等于目标数字 - Can 5 numbers equal a target number using addition, subtraction, or multiplication 不断添加数组中的整数以达到特定的最大值,并使用数组中的那些固定值检查天气是否有可能达到该数字 - Keep adding integers from an array to reach a specific max and check weather its possible to reach this number using those fixed values from an array 如何在arrayList的目标特定数字和运算符 - How to target specific numbers and operators in arrayList's 使用 DOM 时如何到达特定标记? - How could I reach a specific tag while using DOM? 无法使用XMLReader访问特定的XML元素 - Can't reach specific XML element using XMLReader 试图删除大于我的目标数字的数字 - Trying to remove numbers bigger than my target number 返回两个数字的索引,以便它们加起来达到特定目标 - Return indices of the two numbers such that they add up to a specific target 如何创建特定次数的随机数? - How to create random numbers a specific number of times? 生成数字数组的算法,这些数字的总和给出一个特定的数字 - Algorithm to generate an array of numbers that the sum of the numbers gives a specific number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM