Skip to content

Instantly share code, notes, and snippets.

@ofx ofx/Subset Sum
Last active Aug 29, 2015

Embed
What would you like to do?
#include <math.h>
#include <stdio.h>
int S[] = {-7, -3, -2, 5, 8};
int main()
{
unsigned int i, n, a, j, nn, ui;
n = pow(2, sizeof(S) / sizeof(int)) - 1;
ui = sizeof(S) / sizeof(int);
for (i = 0 ; i <= n ; nn = ++i)
{
a = 0;
for (j = 0 ; j < ui ; ++j, nn >>= 1)
{
a += (nn & 0x1) * S[j];
}
if (a == 0)
{
printf("Sum (at %i)\n", i);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.