Implementing and fixing a recursion method in Java -


    public boolean generateproblems(integer[] nums, int start) {      if (start == nums.length) {         return false;     }      (int = -range; < range; i++) {         nums[start] = nums[start] + i;         system.out.println(printarray(nums));         target game = new target(nums, target);         if (game.solutionsize() == difficulty) {             if (!possiblegames.contains(game.getvalues())) {                 possiblegames.add(game.getvalues());             }             return false;         }          if (generateproblems(nums, start + 1)) {             return true;         }         nums[start] = nums[start] - i;      }      return false;  } 

overview: in game, 24, must add, subtract, divide, , multiply 4 numbers reach target, 24. have similar, more abstract class called target, takes array of integers , counts (and solves) solutions reach given target value.

i have class called targetgenerator takes "seed array", target value, range, , number of solutions requested (difficulty). should call method, generateproblems, list of possible games such combination contain difficulty solutions. these possible games bounded initial seed array plus/minus range. (the seed, {2,3,4,5} range 3 bounded {-1,0,1,2} to, not including, {5,6,7,8}.)

however, issue i'm having have no bloody idea why isn't working. .printing out combinations tried in console indicates starts fine, doesn't end well.

with seed of {6,2,3,12}, target of 24, , range of 3:

2 2 3 12  2 -2 3 12  2 -2 -1 12  2 -2 -1 8  2 -2 -1 9  2 -2 -1 10  few more lines later messes up... 2 1 1 12  2 1 1 13  2 1 2 10  2 1 2 6  2 1 2 7  2 1 3 7  ....few hundred more combinations later... 9 -1 -23 -46  9 -1 -23 -45  9 -1 -23 -44  9 -1 -23 -43  ....and i'm sad :( 

it seems me there's wrong happens when reaches end of "loop" (when start == nums.length) , subtracts when it's not supposed to. however, don't have debugging know-how figure out whats wrong.

(i'll give entire code if of want)

i hope not relevant, , solved self, if not here found wrong function. understand, need check posible combinations within given range, if can reach target number or not. if don't have return generateproblems (you can, way did add complexity), need check combinations. how can :

public void generateproblems(integer[] nums, int start)  {      if (start != nums.length)      {         (int = -range; < range; i++)          {             nums[start] = nums[start] + i;              action(nums);             generateproblems(nums, start + 1);                       nums[start] = nums[start] - i;         }     } } 

inside action(nums); goes logic


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -