University of Calgary

problem: zero sum


Zero Sum
[Credit: ACM Programming Contest, adapted]

Consider the sequence of digits from 1 through N (where N <= 9) in increasing order:
1 2 3 4 5 ... N
Your job is to write a program that will calculate the sums and differences of the numbers and find all the sequences of length N that will procude a ZERO sum. Output all the numbers and insert either a (+) for ddition or a (-) for subtraction or a ( ) [blank or null] to run the digits together.
Eg.

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


Updated: August 5, 2005 12:15 AM