0% found this document useful (0 votes)
31 views1 page

C++ Program for Sum Combinations

This C++ code includes a header, declares variables including an array, defines functions to check if elements are in order and do backtracking, calls the backtracking function recursively, and returns 0 at the end.

Uploaded by

Wall Ciment
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views1 page

C++ Program for Sum Combinations

This C++ code includes a header, declares variables including an array, defines functions to check if elements are in order and do backtracking, calls the backtracking function recursively, and returns 0 at the end.

Uploaded by

Wall Ciment
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <pch.

h>
#include <iostream>
using namespace std;

int x[100], n, s = 1;
int OK(int k)
{
if (x[k]<x[k-1])
return 0;
return 1;

}
void bt(int k)
{
for (int i = 1; i <= n - s; i++)
{
s -= x[k];
x[k] = i;
s += x[k];
if (OK)
if (s == n)
{
for (int j = 1; j < k; j++)
cout << x[j] << " + ";
cout << x[k] << endl;
s -= x[k];
}
else bt(k + 1);
}
}
int main()
{
cin >> n;
x[1] = 1;
bt(1);
return 0;
}

You might also like