Created
July 13, 2020 10:56
-
-
Save dionyziz/120d123dd8b7eea04d8e48923b7fc437 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
using namespace std; | |
const int MAX_N = 15; | |
int A[MAX_N]; | |
int n, k; | |
int main() { | |
FILE* fi = fopen("subsetsum.in", "r"); | |
FILE* fo = fopen("subsetsum.out", "w"); | |
fscanf(fi, "%i %i", &n, &k); | |
for (int i = 0; i < n; ++i) { | |
fscanf(fi, "%i", &A[i]); | |
} | |
for (int bitmask = 0; bitmask < (1<<n); ++bitmask) { | |
int sum = 0; | |
for (int i = 0; i < n; ++i) { | |
if (((1<<i) & bitmask) > 0) { | |
sum += A[i]; | |
} | |
} | |
if (sum == k) { | |
for (int i = 0; i < n; ++i) { | |
if (((1<<i) & bitmask) > 0) { | |
fprintf(fo, "%i ", i + 1); | |
} | |
} | |
return 0; | |
} | |
} | |
fprintf(fo, "NO SOLUTION\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment