Untitled
unknown
plain_text
7 months ago
907 B
4
Indexable
#include <stdio.h> #include <math.h> void generateSubset(int num, int n, int x[]) { for (int i = 1; i <= n; i++) { x[i] = 0; } for (int i = n; num != 0; i--) { x[i] = num % 2; num = num / 2; } } int main() { int a[10], x[10]; int n, d, sum, present = 0; printf("Enter the number of elements of set: "); scanf("%d", &n); printf("Enter the elements of set: "); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } printf("Enter the positive integer sum: "); scanf("%d", &d); if (d > 0) { for (int i = 1; i <= pow(2, n) - 1; i++) { generateSubset(i, n, x); sum = 0; for (int j = 1; j <= n; j++) { if (x[j] == 1) { sum += a[j]; } } if (d == sum) { printf("Subset={ "); present = 1; for (int j = 1; j <= n; j++) { if (x[j] == 1) { printf("%d ", a[j]); } } printf("} = %d\n", d); } } } if (present == 0) { printf("Solution does not exist\n"); } return 0; }
Editor is loading...
Leave a Comment