Results 1 to 3 of 3
Thread: Backtracking
- 12-15-2010, 04:45 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
Backtracking
Hello, I got the task to create a program with backtracking, i can not create. Could you write a theory as I do, or write a better program. Entering the example below:
On paper it is written long number 123456789. Insert between some digits signs + or -.
So that you incurred after evaluating the expression given number 100.
For example: 123-45-67 +89 = 100
Write a program that finds and displays all the solutions. Use backtracking.
THANK YOU.
- 12-15-2010, 04:49 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 10
This is a problem that can be solved elegantly with recursion (of which backtracking is an integral part). Until we see an effort from you, I'm afraid you won't get much help, as this is not a "do my homework" forum.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 12-17-2010, 02:08 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
Examples are designed program that addresses the solution. But has several errors. This program should generate 3 ^ 9 = 19683 expressions (when calculating accurately). Instead, it generates a 9841 expressions. It is not a multiple of 3, but its half. The program input of Operators to 0 place - before '1' Strange input of the zero position and generates duplicate values. Can you help? Thank you.
//field in which I to insert operators
static String[] cislice = {"","1","","2","","3","","4","","5","","6","","7", "","8","","9"};
//field with operators
static String [] operatory = {"","+","-"};
...
private static void BacktrackingSto(int krok,int operator){
if(krok>cislice.length-1) return;
//print out genera generated expressions
for(int i=0;i<cislice.length;i++){
System.out.print(cislice[i]);
}System.out.println(); System.out.println();
j++; //number of generated expressions
System.out.println("Backtracking "+krok+", "+operator);
for(int i=0;i<3;i++){
cislice[krok]= operatory[operator];
BacktrackingSto(krok+2,i);
}
}
Similar Threads
-
using backtracking to print all possible orders of numbers
By yotamoo in forum New To JavaReplies: 6Last Post: 12-08-2010, 01:39 AM -
need help with backtracking
By Dumisan in forum Advanced JavaReplies: 9Last Post: 02-17-2010, 02:02 PM
Bookmarks