0

i need help..
i am NOT asking you to do it for me.. i just need some idea coz i can't really understand the problem.. here it is:

Consider the sequence of digits from 1 through N (where N=9) in increasing
order

1 2 3 4 5 . . . N

and insert either a (+) for addition or a (-) for subtraction or a ( ) [blank]
to run the digits together. Now sum the result and see if you get zero.

Write a program that will find all sequences of length N that produce a ZERO
SUM.

Test Case 1

Input
7
Output
1 + 2 - 3 + 4 - 5 - 6 + 7 = 0
1 + 2 - 3 - 4 + 5 + 6 - 7 = 0
1 - 2 + 3 + 4 - 5 + 6 - 7 = 0
1 - 2 - 3 - 4 - 5 + 6 + 7 = 0
1 - 23 + 4 + 5 + 6 + 7 = 0
1 - 23 - 45 + 67 = 0

Test Case 2

Input
8

Output
1 + 2 + 3 + 4 - 5 - 6 - 7 + 8 = 0
1 + 2 + 3 - 4 + 5 - 6 + 7 - 8 = 0
1 + 2 - 3 + 4 + 5 + 6 - 7 - 8 = 0
1 + 2 - 3 - 4 - 5 - 6 + 7 + 8 = 0
1 + 23 - 45 + 6 + 7 + 8 = 0
1 - 2 + 3 - 4 - 5 + 6 - 7 + 8 = 0
1 - 2 - 3 + 4 + 5 - 6 - 7 + 8 = 0
1 - 2 - 3 + 4 - 5 + 6 + 7 - 8 = 0
1 - 23 - 4 + 5 + 6 + 7 + 8 = 0
12 - 34 -56 + 78 = 0

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Jishnu
0

The simplest method is to use brute force. Develop your first program using that, then look out for further optimizations.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.