Pagini recente » Cod sursa (job #3127783) | Cod sursa (job #2384361) | Cod sursa (job #2937780) | Cod sursa (job #2604848) | Cod sursa (job #361870)
Cod sursa(job #361870)
#include<fstream>
using namespace std;
ifstream f("combinari.in");
ofstream g("combinari.out");
typedef struct{
int st[100];
int top;
int h;
} stiva;
stiva stack;
int n;
int init(stiva *s)
{
if((*s).top>1)
(*s).st[(*s).top]=(*s).st[(*s).top-1];
else
(*s).st[(*s).top]=0;
return 1;
}
int succesor(stiva *s)
{
if((*s).st[(*s).top]<n-(*s).h+(*s).top)
{
(*s).st[(*s).top]++;
return 1;
}
else
return 0;
}
int valid(stiva s)
{
return 1;
}
int solutie(stiva s)
{
return s.top==s.h;
}
int tipar(stiva s)
{
for(int i=1;i<=s.h;i++)
g<<s.st[i]<<" ";
g<<endl;
return 1;
}
int bktr()
{
int as;
stack.top=1;
init(&stack);
while(stack.top>0)
{
do{}
while((as=succesor(&stack)) && !valid(stack));
if(as)
if(solutie(stack))
tipar(stack);
else
{
stack.top++;
init(&stack);
}
else
stack.top--;
return 1;
}
}
int main()
{
f>>n;
f>>stack.h;
bktr();
return 1;
}