#include<bits/stdc++.h>
using namespace std;
void combination(int a[], int index, int n, int r)
{
set<int>s;
set<int> :: iterator t;
int sum = 0, i, j;
if(r < 0)
return;
else if(r == 0)
{
for(i=0;i<index;i++)
s.insert(a[i]);
for(t=s.begin();t!=s.end();t++)
sum += *t;
if(sum == n)
{
for(t=s.begin();t!= s.end();t++)
cout << *t << " ";
cout << endl;
}
return;
}
int p;
if(index == 0)
p = 1;
else
p = a[index-1];
for(j=p;j<=n;j++)
{
a[index] = j;
combination(a, index+1, n, r-j);
}
}
void findCombinations(int n)
{
int a[n];
combination(a, 0, n, n);
}
int main()
{
int n;
while(cin >> n)
{
findCombinations(n);
}
return 0;
}
using namespace std;
void combination(int a[], int index, int n, int r)
{
set<int>s;
set<int> :: iterator t;
int sum = 0, i, j;
if(r < 0)
return;
else if(r == 0)
{
for(i=0;i<index;i++)
s.insert(a[i]);
for(t=s.begin();t!=s.end();t++)
sum += *t;
if(sum == n)
{
for(t=s.begin();t!= s.end();t++)
cout << *t << " ";
cout << endl;
}
return;
}
int p;
if(index == 0)
p = 1;
else
p = a[index-1];
for(j=p;j<=n;j++)
{
a[index] = j;
combination(a, index+1, n, r-j);
}
}
void findCombinations(int n)
{
int a[n];
combination(a, 0, n, n);
}
int main()
{
int n;
while(cin >> n)
{
findCombinations(n);
}
return 0;
}
Comments
Post a Comment