3

Hi i really need your help.First of all , think about we got two text files:

  • numbers.txt
  • process.txt

In numbers.txt we have:

4 5 3 2 10 1 45

In process.txt we have:

+ * /

We want to find 45 as a result with using 4 5 3 2 10 1 numbers and + * / to do sums.

Finally we want a txt file like output.txt 4 + (5 + (3 * (2 + (10/1)))) = 45 include the solution like this.

Each number can be used for one time and each process doesn't needed to be used but we cannot use anyother that not in process.txt.

note:if we cannot find the exact result , we have to find a near value.

Can you please help me about that? I'm totaly lost about this. Sorry for my bad english.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
club66
  • 31
  • 1
  • 2
    this question is much too broad. please post the specific areas you are having trouble with, and what you have managed so far. – Mat Apr 06 '11 at 18:54
  • @Mat actually i'm having trouble with how to find the exact result. Would i use for loop for this? @Cybernate yes it's a hw actually. Is this nonallowed? – club66 Apr 06 '11 at 19:02
  • Do you need to use all the given numbers or can you choose freely from them under the constraint that each number can only be used one or less times? Is the given sequence of the numbers to be preserved? Are the parentheses restricted to be as in your example? How many numbers will be given at max and how many and which operators? Please try to make your question more precise. And according to Mat: What have you tried and thought of so far? Homework questions are perfectly allowed but should be marked as this. – TheMorph Apr 06 '11 at 19:49
  • @TheMorph thank you for your interest.we have to use all of the given numbers for once.Sequence of the numbers doesn't need to be conserved.Parentheses restricted to be as x #(x #(x#.... #(x #(x # x )))) [#=operator]. I don't have number limit but i suppose that will be max 50 and also using + - * / operators. As i say i'm totaly lost and try to find a torch , first of all i need to find out how can i create the solution for getting result. Thank you again and again. – club66 Apr 06 '11 at 20:04

2 Answers2

3

Homework hint: Use two Stacks, one holds numbers, one holds operators. Examine the Stacks when adding operators, to see if you can perform any merging of numbers.

Sean Adkinson
  • 8,425
  • 3
  • 45
  • 64
0

you might wanna look here Code Golf: Countdown Number Game there is no java code but you should be able to understand the idea behind

Community
  • 1
  • 1
Jonatan Cloutier
  • 899
  • 9
  • 26