简体   繁体   English

给定一个数组,使用加法和减法找到等于目标值的所有组合(2 个整数)

[英]Given an array, find all combinations (2 integers) using addition and subtraction that equal a target value

I am trying to make a program that solves for all the 2 number combinations of addition and subtraction that equal a target value.我正在尝试制作一个程序来解决等于目标值的所有 2 个加减法组合。

For example, given the array [12,1,9,11,32,19] and the target value twenty, the answers 1+19, 9+11, and 32-12 must be returned.例如,给定数组 [12,1,9,11,32,19] 和目标值 20,则必须返回答案 1+19、9+11 和 32-12。 If there are no possible combinations, the System should print that there are no possible combinations.如果没有可能的组合,系统应该打印没有可能的组合。 Also, every combination must be two numbers ONLY.此外,每个组合只能是两个数字。 Is it possible to do this only in the main class?是否可以仅在主 class 中执行此操作?

import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("What length (in a whole number) would you like the array to be? ");
        int arraySize = sc.nextInt();

        int [] arr = new int[arraySize];
        for (int i = 0; i < arraySize; i++) {
            int remainingNumbers = arraySize - i;
            System.out.println("Please enter " + remainingNumbers + " more integers.");
            arr[i] = sc.nextInt();
        }

        System.out.print("Please enter a target value: ");
        int target = sc.nextInt();
        System.out.println(Arrays.toString(arr));

   // Algorithm here.

    }
}

 import java.util.Scanner; public class Exercise6 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Input first number: "); int num1 = in.nextInt(); System.out.print("Input second number: "); int num2 = in.nextInt(); System.out.println(num1 + " + " + num2 + " = " + (num1 + num2)); System.out.println(num1 + " - " + num2 + " = " + (num1 - num2)); System.out.println(num1 + " x " + num2 + " = " + (num1 * num2)); System.out.println(num1 + " / " + num2 + " = " + (num1 / num2)); System.out.println(num1 + " mod " + num2 + " = " + (num1 % num2)); } }

enter code here

''' Input first number: 32 Input second number: 12 19 + 1 = 20 32 - 12 = 20 9+11= 20' ''' Input first number: 32 Input second number: 12 19 + 1 = 20 32 - 12 = 20 9+11= 20'

Yes, it is possible to do it only in the main class.是的,只能在主 class 中执行此操作。 It is even possible to do it only in the main method (despite doing it an own method is better, easier to understand).甚至可以只在 main 方法中进行(尽管使用自己的方法更好,更容易理解)。 Just two nested for loops;只有两个嵌套for循环; one for the first value, one for the second value.一个用于第一个值,一个用于第二个值。 Inside the inner loop just test if the sum of both values result in the expected result, same for subtraction.在内部循环中,只需测试两个值的总和是否产生预期的结果,减法相同。 Set a boolean to indicate that at least one case was found.设置一个 boolean 表示至少找到一个案例。 At the end print the negative message if the boolean is not set.如果未设置 boolean,则最后打印否定消息。

Sample:样本:

private static boolean findCombinations(int target, int[] values) {  // or ,int... values) {
    boolean found = false;
    for (int i = 0; i < values.length; i++) {
        // second loop
            // tests and print
    }
    return found;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 5个数字可以通过加,减或乘运算等于目标数字 - Can 5 numbers equal a target number using addition, subtraction, or multiplication 给定数组中的元素找到与目标值相等的组合 - Given elements in a array find combinations that equal destination value 如何在整数数组中找到所有对,其和等于给定数字 - How to Find all Pairs in an Array of Integers Whose sum is Equal to a Given Number 给定整数数组,找到所有“最大”子集 - Given an array of integers find all “maximal” subsets 找到给定数组大小的给定数字的所有组合 - find all combinations of given numbers in a given array size 给定一个整数数组和一个总和,任务是找出给定数组的子集是否存在总和等于给定总和的子集 - Given an array of integers and a sum, the task is to find if there exists a subsets of given array with sum equal to given sum 给定整数到字符的映射找到给定整数的所有可能的字符组合 - Given mapping of integers to characters find all possible character combinations of a given integer 根据给定的条件找出2D布尔数组的所有可能组合(使用Java) - Find out all possible combinations of a 2D boolean array given some criteria (using Java) 查找数组中的所有组合 - Find all combinations in an array 在给定整数数组的情况下查找所有增长最长的子序列 - 动态编程 - To find all longest increasing subsequences given an array of integers - Dynamic Programming
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM