我喜欢创造,一只扑腾着翅膀向天空飞翔的乌鸦
乌鸦的程序有问题,高效算法俺就懒得想了,顺手写了个穷举的验证一下你的程序的正确性^_^ #include <stdio.h> #include <alloc.h> void fun2(struct Test *UP); void fun(int N,int SUM,struct Test *UP,int T);
int Sum,Len;
struct Test { int n; struct Test * up; }; main() { struct Test * test0;
test0 = (struct Test *) malloc(sizeof(struct Test));
test0->n=0; test0->up=NULL; scanf("%d%d",&Sum,&Len); printf("Sum=%d Len=%d\n\n",Sum,Len); fun(1,1,test0,2); fun(-1,-1,test0,2);
} void fun(int N,int SUM,struct Test *UP,int T) { struct Test * test0; test0 = (struct Test *) malloc(sizeof(struct Test)); test0->n=N; test0->up=UP;
if(T<Len) {
fun(N+1,SUM+N+1,test0,T+1); fun(N-1,SUM+N-1,test0,T+1); }
if(T==Len&&SUM==Sum) { printf(" %d ",test0->n); fun2(test0->up); }
} void fun2(struct Test *UP) { printf(" %d ",UP->n); if(UP->up!=NULL)fun2(UP->up); else printf("\n"); } 把你的算法再说清楚一点,让俺学习学习