The question
Consider the string of digits 123456789
. Consider all arithmetic expression that can be formed by placing +
or -
interspersed within the string. Examples:
1 + 2 - 345 + 67 - 8 - 9 = 292
123456 - 789 = 122667
Write a Java program that uses a stack to find such a combination that has value 2012
.
My Problem
I am stuck with the logic since we have to use two arithmetic operators.
import java.util.*;
public class arithmeticStack {
public static void main (String args[]) {
ArrayList<String> dg = new ArrayList<String>();
Stack<String> digits = new Stack<String>();
int number = 0;
dg.add("1");
dg.add("2");
dg.add("3");
dg.add("4");
dg.add("5");
dg.add("6");
dg.add("7");
dg.add("8");
dg.add("9");
for (int i = 0; i <= dg.size() - 1; i++) {
digits.push(dg.get(i));
}
for (String f : digits){
number += Integer.parseInt(f);
}
while (number == 2012) {
}
}
}