hi i have the next task to solve.
* We are given 5 integer numbers. Write a program that checks if the sum of some subset of them is 0.
* Example: 3, -2, 1, 1, 8 1+1-2=0.
do u have any other ideas then writing all the 31 if statements?
checho
0
Light Poster
Recommended Answers
Jump to PostYou can do this with a pair of loops. Including whitespace, using statements, and console output, I've coded a full program to work with a fixed array of integers in 28 lines.
I'll give you the general description of the looping structure, sort of like pseudocode, and you should …
Jump to PostI feel horrible replying to this thread again without anyone else chiming in, but oddly enough this problem [non-contiguous subsets, not the zero-sum part] has bugged me. As I said, I tackled it recursively before, but I didn't think it was all that efficient of an approach, even though it …
Jump to PostYou might wanna check this article. The subset sum problem is used to demonstrate the NP-Complete complexity class.
Also, hats off to apegram for simultaneously presenting a well written method and demonstrating …
All 9 Replies
apegram
302
LINQ!
Team Colleague
checho
0
Light Poster
checho
0
Light Poster
apegram
302
LINQ!
Team Colleague
apegram
302
LINQ!
Team Colleague
apegram
302
LINQ!
Team Colleague
Geekitygeek
commented:
ncie method, and great way to demonstrate how to get there
+1
ddanbe
commented:
Great work!
+6
Geekitygeek
480
Nearly a Posting Virtuoso
ddanbe
commented:
Thanks for pointing this out, agree about apegram.
+6
ddanbe
2,724
Professional Procrastinator
Featured Poster
checho
0
Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.