[英]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.