Pop_SqStack(&S,&k); /*回溯寻求下一个解*/ k怎么变化?#include#include #define MAXSIZE 100 /*最多元素数*/ typedef int ElemType; typedef struct{ ElemType data[MAXSIZE]; /*栈空间*/ int top; /*栈顶指针*/ }SqStack; void Init_SqStack(

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/11 05:32:47

Pop_SqStack(&S,&k); /*回溯寻求下一个解*/ k怎么变化?#include#include #define MAXSIZE 100 /*最多元素数*/ typedef int ElemType; typedef struct{ ElemType data[MAXSIZE]; /*栈空间*/ int top; /*栈顶指针*/ }SqStack; void Init_SqStack(
Pop_SqStack(&S,&k); /*回溯寻求下一个解*/ k怎么变化?
#include
#include
#define MAXSIZE 100 /*最多元素数*/
typedef int ElemType;
typedef struct{
ElemType data[MAXSIZE]; /*栈空间*/
int top; /*栈顶指针*/
}SqStack;
void Init_SqStack(SqStack *S){S->top=-1;} //栈空
int Empty_SqStack(SqStack *S){
if(S->top==-1)
return 1;
else
return 0;
}
void Push_SqStack(SqStack *S,ElemType x){
if(S->top==MAXSIZE-1){ /*栈满,不能入栈*/
printf(" 栈满!"); exit(0);
}
else /*栈非满,x入栈*/
S->data[++S->top]=x;
}
void Pop_SqStack(SqStack *S,ElemType *x){
if(Empty_SqStack(S)){/*栈空,不能进行出栈*/
printf(" 栈空!");
exit(0);
}
else{ /*把栈顶元素存入变量x*/
*x=S->data[ S->top];
S->top--;
\x05\x05\x05 }
}
void knap(int w[],int t,int n){
SqStack S;
int j,k;
Init_SqStack(&S);
k=0; /*从 0号物品开始扫描*/
do{
while(k=0){ /*序号为 k的物品入栈*/
Push_SqStack(&S,k);
t-=w[k];
}
k++;
}
if(t==0){ /*输出一个解*/
printf("\nresult:\n");
for(j=0;j

Pop_SqStack(&S,&k); /*回溯寻求下一个解*/ k怎么变化?#include#include #define MAXSIZE 100 /*最多元素数*/ typedef int ElemType; typedef struct{ ElemType data[MAXSIZE]; /*栈空间*/ int top; /*栈顶指针*/ }SqStack; void Init_SqStack(
k=S->data[ S->top];