Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Outputs all possibilities to put + or - or nothing between the numbers 1,2,…,9 (in this order) such that the result is 100.
- package defaultpackage;
- import javax.script.ScriptEngine;
- import javax.script.ScriptEngineManager;
- import javax.script.ScriptException;
- class Main {
- static int[] pos_counter = {0,0,0,0,0,0,0,0};
- static Boolean running = true;
- public static void main(String[] args) {
- int counter = 0;
- while(running) {
- increment();
- oobcheck();
- try {
- counter = (int) calculate(printeq());
- } catch (ScriptException e2) {
- e2.printStackTrace();
- }
- if(counter==100) {
- System.out.print(printeq());
- System.out.print("=");
- System.out.println(100);
- }
- }
- }
- public static int[] increment(){
- int stepcounter = 7;
- pos_counter[7]++;
- while(stepcounter>= 0&&running){
- if(pos_counter[stepcounter]==3&&running){
- if(stepcounter-1 == -1){
- running = false;
- } else{
- pos_counter[stepcounter-1]++;
- pos_counter[stepcounter]=0;
- }
- }
- stepcounter--;
- }
- return pos_counter;
- }
- public static double calculate(String s) throws ScriptException{
- ScriptEngineManager mgr = new ScriptEngineManager();
- ScriptEngine engine = mgr.getEngineByName("JavaScript");
- return (double) engine.eval(s);
- }
- public static void readout() {
- int stepcounter = 0;
- while(stepcounter <= 7) {
- System.out.print(pos_counter[stepcounter]);
- stepcounter++;
- }
- System.out.println("");
- }
- public static String printeq() {
- int stepcounter = 0;
- String read = "";
- while(stepcounter <= 7) {
- read = "" + read + (stepcounter+1);
- if(pos_counter[stepcounter] == 0){
- }else if(pos_counter[stepcounter] == 1) {
- read = read + "+";
- }else if(pos_counter[stepcounter] == 2) {
- read = read + "-";
- }
- stepcounter++;
- }
- read = read + "9";
- return read;
- }
- public static void oobcheck(){
- int stepcounter = 0;
- Boolean checker = true;
- while(checker&&stepcounter <= 7) {
- if(pos_counter[stepcounter] == 2) {
- stepcounter++;
- checker = true;
- }else{
- checker = false;
- }
- if(stepcounter==7){
- if(pos_counter[7] == 2) {
- running = false;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement