Learn to Code
John F. Dumas
contact me | resume | how it works | example programs | testimonials | main page
Java - Chosen Sum
► Problem Description: Given the numbers: 1 -> 9, and that between each number will either be nothing, a '+' or a '-', find all the ways to sum to a chosen value.
► Example:
1+2+3+4+5-6-7+8-9 = 1 1+2+3+4-5+6+7-8-9 = 1 1+2+3+45-67+8+9 = 1 1+2+3-4-5-6-7+8+9 = 1 1+2-3+4-5-6+7-8+9 = 1 1+2-3-4+5+6-7-8+9 = 1 1+2-3-4+5-6+7+8-9 = 1 1+2-34+56-7-8-9 = 1 1+23+4-5+67-89 = 1 1+23+45-67+8-9 = 1 1+23-4-5-6-7+8-9 = 1 1+23-45-67+89 = 1 1-2+3+4-5+6-7-8+9 = 1 1-2+3+4-5-6+7+8-9 = 1 1-2+3-4+5+6-7+8-9 = 1 1-2+34-56+7+8+9 = 1 1-2-3+4+5+6+7-8-9 = 1 1-2-3-4+5-6-7+8+9 = 1 1-2-3-4-5+6+7-8+9 = 1 1-2-3-45+67-8-9 = 1 1-23+4+5+6+7-8+9 = 1 1-23+45+67-89 = 1 1-23-4+5-67+89 = 1 1-23-45+67-8+9 = 1 12+34+5-67+8+9 = 1 12-34+5-6+7+8+9 = 1 ------------------- Total Solutions: 26 1+2+3-4+5+6+78+9 = 100 1+2+34-5+67-8+9 = 100 1+23-4+5+6+78-9 = 100 1+23-4+56+7+8+9 = 100 12+3+4+5-6-7+89 = 100 12+3-4+5+67+8+9 = 100 12-3-4+5-6+7+89 = 100 123+4-5+67-89 = 100 123+45-67+8-9 = 100 123-4-5-6-7+8-9 = 100 123-45-67+89 = 100 ------------------- Total Solutions: 11
► Source Code