#include "stdio.h"
#define maxline 200
int comb2(int n, int m)
{
int stack[maxline][3], top=1,s1,s2;
if (n<m|| n<0||m>0)
{
return (-1);
}
stack[top][1]=0;
stack[top][2]=n;
stack[top][3]=m;
do
{
printf("top=%d %d %d %d\n",top,stack[top][1],stack[top][2],stack[top][3]);
if(stack[top][1]==0)
{
top++;
stack[top][1]=0;
stack[top][2]=stack[top-1][2]-1;
stack[top][3]=stack[top-1][3];
if(stack[top][3]==0||stack[top][2]==stack[top][3])
stack[top][1]=1;
}
if(top>=2&&stack[top][1]>0&&stack[top-1][1]==0)
{
top++;
stack[top][1]=0;
stack[top][2]=stack[top-2][2]-1;
stack[top][3]=stack[top-2][3]-1;
if(stack[top][3]==0||stack[top][2]==stack[top][3])
stack[top][1]=1;
}
if(top>2&&stack[top][1]>0&&stack[top-1][1]>0)
{
s1=stack[top][1];
s2=stack[top-1][1];
top=top-2;
stack[top][1]=s1+s2;
}
}
while(top>1)
return(stack[1][1]);
}
main()
{
int n=5,m=3;
printf("comb: %d\n", comb2(n,m));
getch();
}
----------------解决方案--------------------------------------------------------
while(top>1);
----------------解决方案--------------------------------------------------------
晕 谢谢了
----------------解决方案--------------------------------------------------------