Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #pragma warning(disable:4996)
- int st[100], vf;
- int w, n, a[100];
- int maxSolutionElementValue, max_nivele;
- void Init(int i)
- {
- st[i] = 0;
- }
- int Succesor(int k) {
- if (st[k] < maxSolutionElementValue)
- {
- st[k]++;
- return 1;
- }
- else
- {
- return 0;
- }
- }
- int Solution(int k)
- {
- int suma = 0,i;
- for (i = 1; i <= k; i++)
- suma += a[st[i]];
- if (suma == w)
- return 1;
- else
- return 0;
- }
- int Valid(int k)
- {
- int i;
- for (i = 1; i < k; i++)
- {
- if (st[i]>=st[k])
- {
- return 0;
- }
- }
- return 1;
- }
- void Print(int k) {
- int i;
- for (i = 1; i <= k; i++)
- {
- printf("%d ", a[st[i]]);
- }
- printf("\n");
- }
- void Back()
- {
- int iS, iV, i, j, k, suma, poz;
- vf = 1;
- Init(vf);
- while (vf > 0)
- {
- iS = 0;
- iV = 0;
- do {
- iS = Succesor(vf);
- if (iS)
- {
- iV = Valid(vf);
- }
- } while (iS && !iV);
- if (iS)
- {
- if (Solution(vf))
- {
- Print(vf);
- }
- else
- {
- vf++;
- Init(vf);
- }
- }
- else
- {
- vf--;
- }
- }
- }
- void main()
- {
- int i;
- printf("Dati numarul de elemente: ");
- scanf("%d", &n);
- for (i = 1; i <= n; i++)
- {
- printf("a[%d]= ", i);
- scanf("%d", &a[i]);
- }
- printf("Dati numarul care trb obtinut prin suma: ");
- scanf("%d", &w);
- maxSolutionElementValue = n;
- max_nivele = n;
- Back();
- }
Advertisement
Add Comment
Please, Sign In to add comment